Infinite Loops
Here’s the code for a simple while
loop:
|
|
What do you think will happen if we run this code?
This is an example of an infinite loop, which is a set of code that repeats forever.
By using the operator, -=
, the value of the variable decreases every iteration. The expression num < 21
will always
return True
, so the while
loop will NEVER stop.
Coding Infinity
Simple mistakes in the code create infinite loops, and everyone accidentally creates one from time to time.
When this happens to you, holding down control-c
will usually force your
program to stop.
Infinite loops are usually created from small typos or missing statements. These mistakes set up a situation where the ending condition cannot be reached.
Here’s another infinite while loop. The program is supposed to print a decreasing total until that total reaches 0.
|
|
In this case, the update statement is correct (line 5), but the error occurs
in line 4. Instead of using total
in the boolean expression, we used
start_value
, which never gets updated in the loop.
Check Your Understanding
The following code contains an infinite loop. Which is the BEST explanation for why the loop does not end?
|
|
num
starts at 10 and increases by 1 each time through the loop, so it will always be positive.answer
starts at 1 and increases bynum
each time, so it will always be positive.- You cannot compare
num
to 0 in awhile
loop. You must compare it to another variable.