While Loops
There is another Python keyword that can also be used for iteration—while.
Unlike a for loop, which defines a loop variable and a specific starting
and ending point, a while loop uses a single condition to determine whether
or not to continue running.
This condition controls the iteration. The body of a while loop repeats as long as the expression
evaluates to True.
While Loop Syntax
The general syntax of a while loop looks like this:
while boolean expression:
loop body
A while loop continues to repeat as long as the boolean expression
evaluates to True. This condition usually includes a value or variable
that is updated within the loop. Eventually, the condition becomes False,
and the loop stops.
Just like with for loops, the body of a while loop must be indented,
and it may contain any number of statements.
Example
The following loop repeats until total < 1000 returns False:
| |
Control Flow
We can visualize the program flow of a while loop as follows.
- Python evaluates the condition, which returns a value of
TrueorFalse. - If the condition is
False, exit thewhileloop and continue the program at the next statement after the loop body. - If the condition is
True, run the loop body and then go back to step 1.
For Loops Rewritten as While Loops
We can use while to create any type of loop we want, including anything we
previously did with a for loop. For example, consider our first for
loop that printed the numbers 0 - 20:
| |
This can be rewritten as a while loop:
| |
Instead of using the range function to produce the values for num, we
need to produce them ourselves in a while loop. To do this, before
starting the loop. In this example, we create the variable num and assign it a value of
0 in line 1. Every iteration, line 5 increases num by 1. Eventually, num
increases enough to make the condition num < 21 false, and the loop ends.
num plays the same role as the loop variable in the for loop, but we
need to manage its value ourselves. The for and while loops in the
examples above do exactly the same thing, but they solve the task slightly
differently.
If we imagine giving instructions to someone, the for and while
approaches might sound something like:
- For: Repeat this task for each number 0 - 20.
- While: Repeat this task as long as the number is less than 21.
Input Validation
while and for loops serve the same purpose—to repeat a block of code
multiple times. However, while loops are more flexible than for loops.
If you don’t know how many times the loop needs to run, a while loop is the
best fit. Let’s look at one example of this.
This program is an example of input validation, which checks what the user enters to make sure it won’t cause errors down the line.
In this case, the computer prompts the user to enter a positive number. If
they enter 0 or any negative number, then they see an error message and
are prompted again within the body of the loop. If the user keeps entering
invalid numbers, the loop continues to iterate. As soon as the user chooses
a valid number, the loop ends.
| |
This example shows the additional flexibility provided by while loops.
for loops iterate a specific number of times, but in this case we have no
way of knowing how many times we need to prompt the user for a number. By
setting a single condition (num_choice <= 0) we can keep the while loop
going until the condition returns False.
Which Loop To Use?
Each time we write a for statement, we tell Python exactly how many times
the loop body must repeat. for char in "Hello" repeats once for each letter
in the string (5 times). Similarly, for value in range(10) repeats 10
times, with value assigned the numbers 0 - 9.
Even when we use variables in range(start, stop, step), these variables
store specific values. Python knows exactly how many times to repeat the loop
body, and this is called definite iteration. The starting and ending points
of are set inside the for statement.
When we write a while loop, we give Python a condition to evaluate. When
the condition returns False, the loop stops. We do NOT need to know how
many times to repeat the loop. It will keep going as long as necessary.
As we saw in the input validation example, we cannot not know ahead of time how
many tries the user will need. Since a for loop repeats a specific number
of times, it will not work for this case. Instead, a while loop works
better. Whether the user needs 1, 2, 10 (or more) tries, the loop operates only
as long as it has to.
Indefinite iteration refers to the case where we do not know how many times a loop needs to repeat.
So which type of loop should we use in our code? for loops do better when
iterating over a collection or a fixed number of times. while loops get the
job done when we do not know how long it will take to meet a given condition.
Here are some points of comparison between the two types of loops.
for Pros and Cons
- Easier to set up than
whileloops. - Must have a definite start and end point, and these must be declared in the
forstatement. - Can loop through strings and collections without using an index value (e.g.
for char in 'hello':). - Automatically updates the loop variable.
- It is hard to accidentally create an infinite Python
forloop. - Can be used in place of some
whileloops, but not all. - Do not work for input validation.
while Pros and Cons
- More flexible than
forloops. - Any
forloop can be re-written as awhileloop. - Do not need to know beforehand how many times the loop needs to run.
- Can be used for input validation.
whileloops require more work to build.- Making an infinite
whileloop is easy.
Check Your Understanding
Question
Which of the following will cause this while loop to end? Select ALL that apply.
| |
- “Bob3”
- “Anaconda”
- “Willmore Crane Hastings III”
- “Sally”
- “LaunchCode”
Question
If you know the maximum number of times you need a loop to repeat, either for or while will work, but which option is the better choice?
whileloopforloop
Question
You are asked to program a robot to move tennis balls from one box (Box #1) to another (Box #2), one-by-one. The robot should continue moving balls until Box #1 is empty. However, balls may be added to the box after the robot begins its work.
Which type of loop should you use to write the program?
whileloopforloop
Question
You are asked to write a program similar to the one above, but a user gives the robot a specific number of balls to move from Box #1 to Box #2. (You can assume there will always be more than enough balls in the first box).
Which type of loop should you use to write the program?
whileloopforloop
