# 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).

Activity: CodeLens 10.5.1 (csprepeatnumbers_whilecount1)

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:

Activity: CodeLens 10.5.2 (csprepeatnumbers_whilecount2)

## 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:

Activity: CodeLens 10.5.1.1 (csprepeatnumbers_whilecount3)

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:

Activity: CodeLens 10.5.1.2 (csprepeatnumbers_whilecount4)

Or we could double the counter with each iteration:

Activity: CodeLens 10.5.1.3 (csprepeatnumbers_whilecount5)

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.

Activity: CodeLens 10.5.1.4 (csprepeatnumbers_whilecount6)

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.

Activity: CodeLens 10.5.1.5 (csprepeatnumbers_whilecount7)

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.

You have attempted of activities on this page