Infinite loop prevention in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we write loops, it is important to know how long they run. This helps us avoid loops that never stop, called infinite loops.
We want to understand how the number of steps grows as the loop runs.
Analyze the time complexity of the following code snippet.
count = 0
while count < 5:
print(count)
count += 1
This code prints numbers from 0 to 4 by increasing count each time until it reaches 5.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop runs repeatedly.
- How many times: It runs 5 times, once for each number from 0 to 4.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 loops |
| 10 | 10 loops |
| 100 | 100 loops |
Pattern observation: The number of steps grows directly with the input size. If input doubles, steps double too.
Time Complexity: O(n)
This means the time it takes grows in a straight line with the input size.
[X] Wrong: "The loop will always stop quickly no matter what."
[OK] Correct: If the loop condition never changes, the loop can run forever, causing an infinite loop.
Understanding how loops grow and stop is a key skill. It shows you can write safe code that finishes and does not get stuck.
"What if we forgot to increase count inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand loop condition
An infinite loop occurs when the condition controlling the loop never changes to false.Step 2: Identify cause of infinite loop
If the loop condition never becomes false, the loop keeps running forever.Final Answer:
The loop condition never becomes false -> Option CQuick Check:
Infinite loop = condition never false [OK]
- Thinking break causes infinite loops
- Assuming loops run only once cause infinite loops
- Ignoring the loop condition
Solution
Step 1: Check each loop's condition and body
while True: print('Hello') loops forever because condition is always True with no break. while x < 5: x += 1 increases x, so condition will become false. while x == 0: pass loops forever because x never changes. while x > 0: print(x) loops forever because x is never changed.Step 2: Identify loop that ends
Only while x < 5: x += 1 changes x to eventually stop the loop.Final Answer:
while x < 5: x += 1 -> Option BQuick Check:
Change loop variable to end loop [OK]
- Choosing loops with constant True condition
- Ignoring variable updates inside loop
- Assuming print stops loops
i = 0
while i < 3:
print(i)
i -= 1Solution
Step 1: Analyze loop condition and variable change
i starts at 0 and loop runs while i < 3. Inside loop, i decreases by 1 each time.Step 2: Determine if loop ends
Since i decreases, it will go 0, -1, -2, ... always less than 3, so loop never ends.Final Answer:
0 -1 -2 ... (infinite loop) -> Option DQuick Check:
Variable moves away from exit condition = infinite loop [OK]
- Assuming i increases automatically
- Thinking loop stops after first print
- Confusing syntax errors with logic errors
count = 5
while count > 0:
print(count)Solution
Step 1: Check loop condition and body
The loop runs while count > 0, but count is never changed inside the loop.Step 2: Identify missing update
Without decreasing count, the condition stays true forever, causing infinite loop.Final Answer:
Missing decrement of count -> Option AQuick Check:
Loop variable must change to end loop [OK]
- Thinking increment needed instead of decrement
- Assuming loop ends automatically
- Confusing syntax errors with logic errors
Solution
Step 1: Check loop conditions and variable updates
num = 1 while num <= 5: print(num) never changes num, so infinite loop. num = 1 while num <= 5: print(num) num += 1 increases num, stopping at 6. num = 1 while num < 5: print(num) num -= 1 decreases num, moving away from stop condition. num = 5 while num > 0: print(num) num += 1 increases num starting at 5, so loop never ends.Step 2: Confirm correct loop behavior
Only num = 1 while num <= 5: print(num) num += 1 prints 1 to 5 and stops correctly.Final Answer:
num = 1 while num <= 5: print(num) num += 1 -> Option AQuick Check:
Update loop variable toward exit condition [OK]
- Forgetting to update loop variable
- Updating variable in wrong direction
- Using wrong loop condition
