8.2. Infinite Loops¶
Getting a computer to repeat a set of statements is simple. Sometimes it can be tricky to get it to stop. Remember that a while loop will execute as long as the logical expression is true. What happens if the logical expression is always true?
So, here’s a program that loops forever.
while 1 == 1: print("Looping") print("Forever")
1 will always be equal to
1, the two
1 == 1 tests if 1 is equal to 1. Remember that
x = 3 sets the value of x to 3, it doesn’t test if x is equal to 3. To do that use
x == 3.
We ran the following code in a form of Python where we could stop the computer easily:
1>>> while 1==1: 2 print ("Looping") 3 print ("Forever") 4Looping 5Forever 6Looping 7Forever 8Looping 9Forever 10Looping 11Forever
(We stopped the computer around this point.)
Discuss topics in this section with classmates.