This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

While Loops¶

A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true. The following is the abstract syntax of a while loop block.

CONSTRUCTION: While Loop

while <logical expression>:
# Code block to be repeated until logical statement is false
code block


When Python reaches a while loop block, it first determines if the logical expression of the while loop is true or false. If the expression is true, the code block will be executed, and after it is executed, the program returns to the logical expression at the beginning of the while statement. If it is false, then the while loop will terminate.

TRY IT! Determine the number of times 8 can be divided by 2 until the result is less than 1.

i = 0
n = 8

while n >= 1:
n /= 2
i += 1

print(f'n = {n}, i = {i}')

n = 0.5, i = 4


WHAT IS HAPPENING?

1. First the variable i (running count of divisions of n by 2) is set to 0.

2. n is set to 8 and represents the current value we are dividing by 2.

3. The while-loop begins.

4. Python computes n >= 1 or 8 >= 1, which is true so the code block is executed.

5. n is assigned n/2 = 8/2 = 4.

6. i is incremented to 1.

7. Python computes n >= 1 or 4 >= 1, which is true so the code block is executed.

8. n is assigned n/2 = 4/2 = 2.

9. i is incremented to 2.

10. Python computes n >= 1 or 2 >= 1, which is true so the code block is executed.

11. n is assigned n/2 = 2/2 = 1.

12. i is incremented to 3.

13. Python computes n >= 1 or 1 >= 1, which is true so the code block is executed.

14. n is assigned n/2 = 1/2 = 0.5.

15. i is incremented to 4.

16. Python computes n >= 1 or 0.5 >= 1, which is false so the while-loop ends with i = 4.

You may have asked, “What if the logical expression is true and never changes?” and this is indeed a very good question. If the logical expression is true, and nothing in the while-loop code changes the expression, then the result is known as an infinite loop. Infinite loops run forever, or until your computer breaks or runs out of memory.

EXAMPLE: Write a while-loop that causes an infinite loop.

n = 0
while n > -1:
n += 1


Since n will always be bigger than −1 no matter how many times the loop is run, this code will never end.

You can terminate the infinite while loop manually by pressing the interrupt the kernel - the black square button in the tool bar above, or the drop down menu - Kernel - Interrupt in the notebook. Or if you are using the Python shell, you need to press cmd + c on Mac or ctrl + c on PC.

Can you change a single character so that the while-loop will run at least once but will not infinite loop?

Infinite loops are not always easy to spot. Consider the next two examples: one infinite loops and one does not. Can you determine which is which? As your code becomes more complicated, it will become harder to detect.

EXAMPLE: Which while-loop causes an infinite loop?

# Example 1
n = 1
while n > 0:
n /= 2

# Example 2
n = 2
while n > 0:
if n % 2 == 0:
n += 1
else:
n -= 1


Answer: The first example will not infinite loop because eventually n will be so small that Python cannot tell the difference between n and 0. More on this in Chapter 9. The second example will infinite loop because n will oscillate between 2 and 3 indefinitely.

Now we know two types of loops: for-loops and while-loops. In some cases, either can be used equally well, but sometimes one is better suited for the task than the other. In general, we should use for-loops when the number of iterations to be performed is well-defined. Conversely, we should use while-loops statements when the number of iterations to be performed is indefinite or not well known.