While loops

Coding with Python

🕑 This lesson will take about 20 minutes

Loops are used to repeat a set of instructions in a program. Loops are an example of a repetition control structure.

There are two main types of loops used in Python:

  • while loop – a while loop allows you to repeatedly execute a block of code as long as a specified condition is true. The condition is a Boolean expression (that can evaluate to True or False) which determines whether the loop should continue executing or not. As long as the condition evaluates to True, the loop will keep running. Once the condition evaluates to False, the loop exits, and the program continues with the next statement after the while loop.

  • for loop – a for loop allows you to repeatedly execute a block of code a specified number of times (eg. repeat an instruction 10 times). A for loop has a counter or range that specifies how many times the block of code should repeat. For loops can also be used to go process elements in a sequence eg. search through the elements in a list, or characters in a string, one by one. We will look at for loops in the next lesson and focus on while loops in this lesson.

While loops

While loops have two parts:

  • The condition - a Boolean expression (meaning a condition that is tested and can evaluate to either True or False, for example, x > y ) determines whether the code block in the loop should continue executing or not. While the condition evaluates to True, the loop will keep running. Once the condition becomes False, the loop exits, and the program continues with the next statement after the while loop. The condition is tested at the start of the loop block and after each time the loop block has been executed (after each iteration of the loop).

  • The code block - the code block contains the statements that will be executed repeatedly as long as the condition is true. The code block inside the while loop is indented. If, at any point, the condition evaluates to False, then the code block will not run and the program will continue with the next code statement after the while loop.

Example 1 - while loop with a counter

In the example code below, there is a while loop that repeats a block of code while a counter variable is less than 10. A counter variable is created and is initialised to 0. The loop condition is while counter < 10 so the block of code inside the loop will repeat until the counter is no longer less than 10. The code block displays the counter value and increases the counter by 1 each time it runs. So, the loop will display the numbers 0 to 9.

In the example above, it is important that the counter variable is increased each time the loop repeats, so that the specified condition eventually evaluates to False. Loops must have a condition that can eventually evaluate to False, otherwise you will have an infinite loop that will cause the program to crash. Try removing the line counter += 1 (this line of code increases the counter by 1. It is a shorter way of writing counter = counter + 1 ). What happens when you do this? What happens if you change the loop’s condition to something like while counter >= 0 ?

Example 2 - while loop without a counter

You can specify any type of Boolean expression in a while loop, just like you can in an if statement, for example:

  • while score < 50

  • while playerHealth > 0

  • while not gameOver

  • while userConnected

  • while secretPassword != "oranges"

In the example below, the user is asked to input a secret password. While the user’s input does not match the correct password, they will repeatedly be asked to enter it again. Once the correct password is entered, the code block in the will end program will display a confirmation message.

Next lesson: For loops