While Loops

Resources

The first type of loop to explore in Python is the while loop. A while loop uses a Boolean expression, and will repeat the code inside of the loop as long as the Boolean expression evaluates to True. These loops are typically used when we want to repeat some steps, but we aren’t sure exactly how many times it must be done. Instead, we typically know that there is some condition that must be True while we repeat the code, and once it turns False we can stop looping and continue with the program.

The general syntax for a while loop in Python is shown here:

while <boolean expression>:
    <block of statements>

Notice that this syntax is very similar to an if statement. We start with the keyword while, followed by some sort of a <boolean expression> that will evaluate to either True or False. The Boolean expression in Python does not need to be placed inside of parentheses, unlike in pseudocode. After the Boolean expression is a single colon :. Then, starting on the next line and indented one level is a <block of statements> that will be executed when the Boolean expression evaluates to True. Once again, just like in an if statement, we know which statements are part of the block within a while loop based solely on indentation.

Just like in pseudocode, it is possible for the Boolean expression to evaluate to False initially, which will bypass the loop entirely and never execute the code inside of it. Likewise, if we aren’t careful, we can also write a loop where the Boolean expression will always be True, and the loop will run infinitely, causing our program to lock up and never terminate.

Code Tracing Example

To truly understand how a while loop works in Python, let’s go through a simple code tracing example in Python Tutor. Consider the following Python program:

def main():
    x = int(input("Enter a number from 0 to 100: "))
    total = 0
    while total % 100 != x:
        total = total + 9
    print("The smallest multiple of 9 that ends in {} is {}".format(x, total))


main()

See if you can figure out what this program does before moving on!

As always, you can copy and paste this code in the tutor.py file in Codio, or click this Python Tutor link.

When we load this code in Python Tutor, we should see the usual default state:

Tutor 1 Tutor 1

The first couple of steps will simply find the main() method and record it in the global frame, and then call the main() method from the bottom of the program

Tutor 3 Tutor 3

The first line of the program will prompt the user for input and store it in the variable x. For this example, let’s assume the user inputs the string "27".

Tutor 4 Tutor 4

Therefore, we’ll store the value $27$ in the variable x. The next line will store the value $0$ in the variable total, as shown here:

Tutor 6 Tutor 6

At this point, we’ve reached the beginning of the while loop. So, we’ll need to determine if the Boolean expression evaluates to True or False. In this case, the value of total % 100 is equal to $0$, so it is not equal to the value stored in x. Therefore, the Boolean expression is True, and we should enter the loop.

Tutor 7 Tutor 7

Inside of the loop, we are simply adding $9$ to the value in total. So, we’ll see that variable is updated, and the execution pointer goes back to the top of the loop.

Tutor 8 Tutor 8

Now that we are back at the top of the loop, we need to check the Boolean expression again. This time the value of total % 100 is $9$, which still isn’t equal to the value stored in x, so we’ll enter the loop again.

Tutor 9 Tutor 9

We’ll add $9$ to the total here, and jump to the top again.

Tutor 10 Tutor 10

Once again, we will check the Boolean expression and see that it is still True, so we’ll enter the loop and add $9$ to the total yet again before repeating the process from the top of the loop. At this point, we should see this state in Python tutor.

Tutor 12 Tutor 12

When we compute total % 100, we get the value $27$, which is the same as the value stored in x. This will make the Boolean expression evaluate to False, so we can skip the loop and jump to the bottom of the program.

Tutor 13 Tutor 13

Here, we are simply printing out the output, and then the program will terminate.

Tutor 14 Tutor 14

A full animation of this program’s execution in Python Tutor is shown here.

Tutor Animation 7 Tutor Animation 7

Tracing the execution of a while loop is quite simple, especially with tools such as Python Tutor to help us make sure we’re updating the variables in the correct order.