I. While Loop
1. While loop
(1) While loops execute a block of code as long as some condition is true.
(2) If we start x at 0, and count while x < N, we iterate N times (with x as 0,1,..., N-1)
2. Hint on Creating While Loops
Some of the trickier parts of writing a correct while loop are:
(1) Where does it start?
ex) “what gets initialized before the loop starts? And to what value?”
(2) When does it go?
ex) “what goes in the parentheses?”
(3) How do you get there?
ex) “what’s the increment or decrement?”
Answer these questions FIRST before you worry about the rest of what goes in the loop.
3. While Syntax
While (condition) { Statement 1; Statement 2; } |
(1) The condition may be any expression that can be interpreted as a boolean, and often expresses some limit on the value of a variable.
(2) The “body” of the while loop may contain any number of statements, surrounded by the curly braces.
(3) It’s very common in C++ to count starting with zero. If we start at x=0 and count while x < N, we iterate N times (0,1,...,N-1).
EXERCISE) While Loops
Write a code that uses a while loop to print out the following:
9 7 5 3 1 done!
(each number is followed by a space.)
II. Increment and Decrement
C++ provides special operators (+=, ++, -=, and --) for the common tasks of increasing or decreasing a variable.
1. Increment
(1) Increase a variable by some amount. There are two increment operators:
2. Decrement
(1) Decrease a variable by some amount. There are two decrement operators:
* You can also write x++ or x--, but those technically do something a bit different. Prefer to use the ++x and –x versions.
EXERCISE) Increment and Decrement Operators
Modify the code below to replace the update expressions in each of the loops with an equivalent using the shorthand ++ and – operators. The overall output of the program should not change.
III. For Loops
1. For loops
(1) Many loops fall into a particular pattern:
(Step 1 is performed only once. Step 2-4 are repeated until the condition is false.)
(2) C++ provides the for loop specifically for this pattern.
(Step 1 is performed only once. Step 2-4 are repeated until the condition is false.)
2. For loops: Syntax and Control Flow
Note the semicolons used to separate parts of the for.
for ( Initialize ; Condition ; Increment) { Body } |
* Use semicolons to separate these.
EXERCISE) Converting a while loop to a for loop
Write a code that uses a for loop instead of a while loop, while still writing the same output to cout:
1 2 4 8 16 32 done!
(each number is followed by a space.)
IV. Nested Loops
Block scope applies to any block of code, including the bodies of control flow structures like if, for, and while.
The top of a for loop is treated as if it were inside the loop body.
If you need to use a variable after the loop, you need to declare it outside the loop.
Just like you can have nested if statements, you can have nested loops:
여기서 첫번째 outer loop 가 돌아갈 때 inner loop 의 I = 1이 되고 그게 3번이 돌아가면 각각 1*0=0, 1*1=1, 1*2=2 가 되는 것임. 그리고 두번째 outer loop 가 돌아가면 I = i+2 가 되고 2*0=0, 2*1=2, 2*2=4 이렇게 되어서 outer loop 가 총 5번 돌아가게 됨.
EXERCISE) Converting a while loop to a for loop
Write a code below to print out a “triangle” of ‘x’ characters with a size specified by the variable N. For example, if N is set to 5, the program should print out five rows of X’s, each row with an increasing number of X’s up to 5:
=======================
X
XX
XXX
XXXX
XXXXX
=======================
V. Break and Continue
1. Break
(1) There may be times when you need to end a loop early.
(limit = 5.0)
(2) Use the break function to exit the loop gracefully.
2. Continue
(1) There may be times when you need to “skip a loop”.
(2) Use the continue function to end the loop at that point and go back to the top of the loop for the next iteration.
EXERCISE) Application: DNA Pattern Matching
Write a program that searches for a specific pattern in a DNA sequence. Both the DNA sequence and the pattern that we’re looking for are going to be stored as strings.
To find the length of a string, use the length() function: dna.length().
If we want to look at individual characters in out string, we can index into the string using []. However, when we index into a string, C++ starts counting at zero, not one. So, if we wanted to print out the first element of our string, we would use index 0:
If there are three characters in our string, then we can use indices 0, 1, and 2.
The program should output:
Pattern 1: 1
Pattern 2: 2
Pattern 1: 7
Pattern 2: 8
IV. Summary
1. Iteration is a form of control flow that traverses through code systematically based on some defined order or condition. ( for loops and while loops)
2. Increment and decrement operators (+=, ++, -=, and --) increase or decrease variables by some amount. They are often used in loops.
3. Counting starting with zero is a common pattern in C++.
4. Loops can be nested. Variables declared inside a loop only have scope within that loop.
5. break allows you to end a loop early. Continue ends the loop at the point and goes back to the top of the loop for the next iteration.
6. To use strings in C++, you need to include the string library. The length() function tells you how long a string is. You can index into strings using [].
'[Umich] COE Core > ENGR 101 (Matlab, C++)' 카테고리의 다른 글
[Notes] Ch.16 Strings, Streams, and I/O (Runestone) (0) | 2022.11.18 |
---|---|
[Notes] Ch.15 Functions in C++ (Runestone) (0) | 2022.11.18 |
[Notes] Ch.13 More C++ Basics and Branching (Runestone) (0) | 2022.11.18 |
[Notes] Ch.12 Introduction to C++ (Runestone) (0) | 2022.11.18 |
==Incomplete== [Notes] Ch.11 Applying Computing to Society (Runestone) (0) | 2022.11.18 |
댓글