Get startedGet started for free

While loops

1. While loops

For loops and conditional statements are great tools for building a custom workflow. Let's examine one more technique, while loops.

2. If statement

Before we discuss while loops, let's reiterate the flow of an if statement. It checks for a condition, which, if met, triggers an action, and then ends. If the condition is not met, the action is skipped.

3. If statement versus while loop

A while loop is similar to an if statement. It executes code if the condition is True. However, the while loop will continue to execute this code over and over again as long as the condition is true.

4. While loop

We start a while loop with the while keyword followed by a condition and end the line with a colon. The next line is indented and contains the action to perform while the condition is met. While loops are useful for continuous tasks. For example, think of a racing game where a player should accelerate while a button is pressed.

5. While loop

For our recipe scaler, imagine we're adding ingredients to our shopping list one at a time until we've added everything we need. We have a variable called ingredients_to_add, set to 5, representing how many ingredients we still need to add. We create another variable called items_added, equal to zero, to track our progress. We want to keep adding ingredients while we still have items left. This is perfect for a while loop, we know the stopping condition, but not exactly how many steps it will take. Inside the while loop, each time we add an ingredient, we increase items_added by one, using the plus equals syntax, which is the same as saying the variable is equal to itself plus another value. We then print how many ingredients are left by subtracting items_added from ingredients_to_add.

6. Output

The output prints the remaining capacity during each iteration of the loop. It runs down to zero then the while loop exits.

7. A word of caution

Remember, we saw that when using a while loop the code will continually execute as long as the condition is met. What happens if we forget to increment items_added inside the loop?

8. Running forever

The loop will run forever because the condition never changes - ingredients_to_add will always be greater than items_added. This is called an infinite loop, and it's a common bug that can freeze our application.

9. Breaking a loop

To avoid this we have a couple of options. We can use the break keyword inside the loop, which will terminate the code. This can be helpful if we want to perform an action once or add additional code to set the number of times an action is performed. Break can also be used in for loops. If we're already running the code and want to terminate the Python program we can press Control and C on Windows or Command and C on Mac to interrupt it.

10. Conditional statements within while loops

Just like how we can include conditional statements in for loops, we can also do this in while loops! Let's customize our output based on how many ingredients are left. This pattern is common when providing progress updates or status messages to users. Instead of just printing numbers, we can give more helpful feedback. If ingredients_to_add is more than 3, we print "Several ingredients remaining." Using elif, we print "Almost done!" if there are 1 or 2 items left. Otherwise, we print "Shopping list complete!"

11. Conditional statements output

The output now provides meaningful feedback at each stage, and the loop still exits when all ingredients are added.

12. Let's practice!

While you're enjoying the course, let's try out some exercises!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.