Over the last few labs, we’ve explored how we can change the control flow of our programs using conditional statements. A program that doesn’t contain any conditional statements will have a linear control flow. Each time we run the program, it will execute the same steps in the same order, but it may use different data based on input from the user.

When a program contains a conditional statement, its control flow can split into different branches. This means that each program execution may take a different path through the program, but it will eventually reach the end. There is no way to go back and repeat a step that has already been done!

In this lab, we’re going to introduce the other important method for modifying the control flow of our programs - a loop or iteration statement. In programming, a loop is a construct that allows us to repeatedly execute the same piece of code, either for a set number of times, or while a particular Boolean condition is true. Each time we execute the code inside the loop, we call that an iteration of the loop.

Loops are one of the most useful ways we can modify the control flow in our programs. With both loops and conditional statements in our list of available programming skills, we can write programs that execute different branches of code based on a Boolean expression, and we can also repeatedly execute the same piece of code either a defined number of times or while a Boolean expression evaluates to True. This allows us to build some truly useful programs.

In this lab, we’re going to explore ways to use loops in Python. Python supports two different types of loops, while loops and for loops, both of which can be used in a variety of different ways in our programs. We’ll also explore how to use these loops to receive input from the user and handle the case where a user provides invalid input, and finally we’ll look at some ways to test our code containing loops to confirm that they are working correctly and won’t cause issues.

Let’s get started!