Drop links or images here to add them to the editor.

A while loop keeps going until its condition turns false. That is exactly what you want when the program has to wait for the user: keep asking until the password is right, keep guessing until the number comes up. Nobody can say in advance how many times such a loop will repeat.

Very often you know the answer in advance. Print a menu of five options. Ask for three numbers. Count down from ten. The number of repetitions is part of the plan, and the program does not have to work it out.

Python has a second kind of loop for those: the for loop.

for counter in range(10):
    print(counter)

print("Finished")

Run it and you get the numbers 0 to 9, one per line, and then Finished.

Both kinds of loop have a name in the exam specification. A loop that repeats a set number of times is count controlled, and a loop that repeats until something changes is condition controlled. In Python those are for and while.

Reading the first line

Every for loop starts with the same four pieces.

for counter in range(10):

The counter is an ordinary variable, so you can call it what you like. counter and count are clear when the loop is just repeating something, and i is what you will see in a lot of code once loops get shorter. What you cannot do is pick a name that is already in use for something else, because the loop overwrites it on every repetition.

The counter is built in

In a while loop you have to build the counter yourself, in the three places you met in the previous chapter: create it before the loop, compare it in the condition, and change it inside the body. Forget the third one and the loop never ends.

counter = 1

while counter < 5:
    print(counter)
    counter = counter + 1

A for loop does all three for you.

for counter in range(1, 5):
    print(counter)

Both of these print 1, 2, 3, 4. The second one cannot be given an infinite loop by accident, because there is no condition to get wrong and no step to leave out. That is the practical reason to use a for loop whenever you already know how many repetitions you need.

The body is decided by the indentation

This is the same rule as before, and it is still the one that catches people out.

for count in range(3):
    print("Hello")
    print("There")
print("You")

Hello and There are indented, so both of them are inside the loop and both of them run three times. You starts at the left hand margin, so it is not part of the loop and it runs once, after the loop has finished. Move that last line in by four spaces and the output changes completely.

Using the value

The counter is not only there to count. You can use its value inside the body, which is how one short loop prints a whole times table.

number = 2
for count in range(1, 11):
    print(count * number)

The counter takes the values 1 to 10, so the body prints 2, 4, 6 and on up to

  1. Change number to 7 and you get the seven times table from the same four lines.
Help! My code doesn't work!

Make sure that you check for the following things:

  • A count controlled loop begins with for.
  • The first line names a counter variable and gives it a range(...) to work through.
  • The for line has a colon (:) at the end.
  • All the code you want to repeat is indented after the for, and all of it by the same amount. Never mix tabs and spaces in one file.
  • Any code that should run once the loop has finished is not indented.
  • Numbers are wrapped in str() before they are joined onto text with a +.