Counting with a While Loop

It’s easy to have the computer repeat something a specific number of times. We have done this with a for loop and a list of numbers created with the range function as shown below.

(for_counter_b)

We can also do it with a while loop.

For example, we could have a computer count up from 1 to 10. We will use a counter variable that we will increment inside the loop. Increment means increase the value by one. Note that we continue the loop as long as the counter is less than the desired last value plus one.

(while_count)

    csp-8-3-1: What is the value of counter after the loop finishes executing?
  • 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.

    csp-8-3-2: What does the following code print?

    1
    2
    3
    4
    5
    6
    output = ""
    x = -5
    while x < 0:
        x = x + 1
        output = output + str(x) + " "
    print(output)
    
  • 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 incremented x.
  • -4 -3 -2 -1
  • The value of x is incremented before it is printed so the first value printed is -4.

Side by Side Comparison of a For Loop and a While Loop

Let’s look at these loops side by side. The first line in the for loop creates the variable counter and the list of values from 1 to 10. It then sets counter equal to 1 and executes the body of the loop. In the body of the loop it prints the current value of counter and then changes counter to the next value in the list.

The first line of the while loop creates the variable counter and sets its value to 1. The second line tests if the value of counter is less than 11 and if so it executes the body of the loop. The body of the loop prints the current value of counter and then increments the value of counter. The loop will stop repeating when counter is equal to 11.

a for loop next to an equivalent while loop

Figure 1: A for loop and the equivalent while loop

Which is the best loop to use when you want to execute a loop a known number of times? Which way uses less code or seems less error prone? The problem with using a while loop to repeat code a specific number of times is that you may forget to change the value that you are testing inside the body of the loop and in that case you will have an infinite loop.

The following code is an attempt to show another way to print the values from 1 to 10. However, it currently has an error and is an infinite loop. Fix the code below so that it isn’t an infinite loop.

Note

This example introduces the comparison operator <= which means less than or equal to.

        csp-8-3-4: csp-8-3-3: The following is the correct code for printing a countdown 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.
Just drag the block to the further right to indent.  Click the Check
Me button to check your solution.

counter = 10 while counter >= 0: print(counter) counter = counter - 1
        csp-8-3-6: csp-8-3-5: The following is the correct code for printing the even numbers from 0 to
10, 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.  Just drag
the block to the further right to indent.  Click the Check Me button
to check your solution.

counter = 0 --- while counter <= 10: --- print(counter) --- counter = counter + 2 --- counter = counter + 1 #distractor

Use a while loop to count from 5 to 9. Print the current value in the counter at every interation in the loop.

Next Section - Looping When We Don’t Know When We’ll Stop