Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
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.
for loop automatically moves to the next value as we iterate through a list. This program
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).
while loop, there is not a known list of items we are iterating through. So the
can’t automatically go to the next value. It is up to us to advance the loop. Here is a
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
counter < 11). A loop control variable should be initialized before The
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.
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.