While loops
1. While loops
For loops and conditional statements such as if, elif, and else keywords, are great tools for building a custom workflow. Let's extend our toolkit by examining another 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, as opposed to the if statement, the while loop will continue to execute this code over and over again as long as the condition is true.4. While loop
The syntax for while loops is similar to an if statement. We start 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. They are useful for continuous tasks. For example, think of a racing game where a player should accelerate while a button is pressed. Or monitoring traffic to a website and allowing access while there is less than a predefined threshold of unique IP addresses.5. While loop
Let's show an example in action. We have a variable called stock, set to 10, representing how much stock of an item we have available in an online store. We have another called num-purchases, equal to zero. We want to evaluate whether the number of purchases is below the stock count. Inside the while loop, for every new purchase, we increment the num-purchases variable by one. We then print the remaining stock by subtracting the number of purchases from the stock.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 if don't increment num_purchases by one inside the while loop?8. Running forever
The loop will continue to run forever because the stock is always larger than the number of purchases, meaning the condition is always met!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 just want to do something 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 a 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! Rather than printing how much stock is left, we can customize the print statement based on the remaining stock! If stock is more than seven we print "Plenty of stock remaining". Using elif, we print "Some stock remaining" if there are more than 3 items left. Another elif checks whether any stock is left, printing "Low stock!". Otherwise, we print "No stock!"11. Conditional statements output
The output is now customized depending on the state at that point in the while loop, and it still exits when stock reaches zero.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.