Sie sind auf Seite 1von 24

Looping

Iteration

One thing that computers do well is repeat


commands
Programmers use loops to accomplish this
3 kinds of loops in C++
for loop
while loop
do while loop
Criteria for loops

1. Usually have some initial condition


Starting a counter
Beginning in a certain state

2. Must have a test to continue

3. Must make progress towards finishing


The for loop

Good when you know exactly how many times you


need to execute something
Has format:
for (<initialization>; <test to continue>; <increment>) {
// everything in here is what is repeated
// over and over again
}
Initialization is where the counter is given a starting value
The test determines whether or not to continue
The increment can be any amount, including negative, and
occurs after the loop statements execute
Example

Print counter value up to 1000? No problem

int counter;
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n , counter);
}

// Remember, counter++ is the same as


// counter = counter + 1
Why this works

counter

int counter; 1
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

true counter

int counter; 1
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

counter

int counter;
1
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}

Output: Counter =1
Why this works

counter

int counter; 2
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

true counter

int counter; 2
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

counter

int counter;
2
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}

Output: Counter = 2
Why this works

counter

int counter; 3
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

true counter

int counter; 3
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);

}
Why this works

counter

int counter;
3
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}

Output: Counter = 3
Why this works

counter

int counter; 4
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
When will it end?

We see that this will go on for a while


Its a little more interesting later around 1000
Why this works

true counter

int counter; 999


for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

counter

int counter;
999
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}

Output: Counter = 999


Why this works

counter

int counter; 1000


for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

true for last time counter

int counter; 1000


for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works
(are we finished?)

counter

int counter;
1000
for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}

Output: Counter = 1000


Why this works

counter

int counter; 1001


for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
Why this works

false counter

int counter; 1001


for (counter = 1; counter <= 1000; counter++) {
printf(Counter = %d\n, counter);
}
// Exit loop, jump down here and continue

Final Output

Counter = 1
Counter = 2
Counter = 3
Counter = 4
.
.
.
Counter = 999
Counter = 1000

Das könnte Ihnen auch gefallen