10.5. Counting with a While Loop¶
We can also use a while
loop to count through values similar to a for
loop that is
iterating over a range. However, it does involve a little extra work on our part.
A for
loop automatically moves to the next value as we iterate through a list. This program
will set counter
to 1, print it, then change counter
to 2, print that, etc… It continues
counting until it reaches 11 (and stops the loop before printing it).
In a while
loop, there is not a known list of items we are iterating through. So the while
can’t automatically go to the next value. It is up to us to advance the loop. Here is a while
loop that counts from 1 to 10:
10.5.1. Loop Control Variable¶
In the while loop in the program above, counter
is known as the loop control variable.
The loop control variable is a variable that is tested as part of the logical expression
of the while
(counter < 11
). A loop control variable should be initialized before The
while (counter = 1
), tested by the while, and then updated in the loop body.
The last line in the loop: counter = counter + 1
is the update. It is what advances us
to the next value. Without that line, counter
would never change. Try running this version
of the program:
Remember that in assignment (=
) statements, we always do the work on the right side, and
then store the answer into the variable on the left side. Thus in counter = counter + 1
,
we first do counter + 1
which says “add one to the value we have stored in counter”. On its
own, that would not change counter
. To actually change counter, we need to store the answer
back into counter, so we do counter = ...
.
We can do whatever we want to update the counter. We could count from 30 down to 0 by 5:
Or we could double the counter with each iteration:
Where we do the update matters. In this loop, counter
starts at 0. We print that value,
then add 2 to the counter
. When we get to 6, it is printed, then we add 2. Then we go
back to the while
’s test. Since counter < 8
is no longer true, we end the loop. So
we see the value 0,2,4,6 even though the counter ends up reaching 8.
In this loop, the counter starts at 0, but we add 2 to it before we print it the first time.
Thus the first thin printed is 2. Then, when counter gets to 6, we add 2 more to get 8 and
then print that value before we go back up to test the while
’s expression again.
At that point, we realize it is time to stop. Thus this version prints out 2,4,6,8.
Note
A common misconception is that a while loop will stop as soon as it’s logical expression becomes false. A while loop won’t stop in the middle of the body. It is only at the start of each iteration that the logical expression is evaluated to decide “should we do the body again?”
The following is the correct code for printing a countdown that prints from 10 to 0, but it is mixed up. Drag the blocks from the left and put them in the correct order on the right. Don’t forget to indent blocks in the body of the loop.
The following is the correct code for printing the even numbers from 2 to 10, by 2’s but it also includes some extra code that you won’t need. Drag the needed blocks from the left and put them in the correct order on the right. Don’t forget to indent blocks in the body of the loop.
- 1
- Counter is incremented each time the loop executes.
- 10
- The last value to be printed is 10. But, the counter is incremented after the current value is printed.
- 11
- Counter gets incremented to 11 after printing, and then the while loop tests counter, finds counter is not less than 11 and then continues after the body of the loop.
What is the value of counter after the loop finishes executing?
counter = 0
while counter < 11:
print(counter)
counter = counter + 1
- 5 4 3 2 1
- If x starts at -5 how can the first value printed be 5?
- -5 -4 -3 -2 -1
- This would be true if the print statement was before we increased x.
- -4 -3 -2 -1 0
- The value of x is incremented before it is printed so the first value printed is -4.
What does the following code print?
output = ""
x = -5
while x < 0:
x = x + 1
output = output + str(x) + " "
print(output)