While–else behavior in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time it takes to run a while-else loop changes as the input grows.
We want to see how many times the loop runs before it stops or finishes.
Analyze the time complexity of the following code snippet.
count = 0
while count < n:
if count == 5:
break
count += 1
else:
print("Loop finished without break")
This code counts up to n but stops early if count reaches 5. The else runs only if the loop ends normally.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that increases count.
- How many times: min(n, 6) times due to the break at count == 5.
As n grows, the number of loop iterations increases up to 6, then remains constant due to the break.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 (loop runs 3 times, no break) |
| 5 | 5 (loop runs 5 times, no break) |
| 10 | 6 (loop runs 6 times, break when count == 5) |
Pattern observation: The loop runs up to 6 times regardless of n (for large n) due to the early break.
Time Complexity: O(1)
This means the loop runs a constant number of times (at most 6), not growing with n, because it stops early.
[X] Wrong: "The loop always runs n times because it depends on n."
[OK] Correct: The break stops the loop early, so it may run fewer times than n.
Understanding how loops with break and else behave helps you explain code clearly and reason about performance.
"What if we remove the break statement? How would the time complexity change?"
Practice
What happens to the else block in a while loop if the loop ends normally (without a break)?
Solution
Step 1: Understand the while-else structure
Theelseblock after awhileloop runs only if the loop finishes all iterations without encountering abreak.Step 2: Analyze loop ending conditions
If the loop ends normally (condition becomes false), theelseblock executes. If abreakoccurs, it skips theelse.Final Answer:
The else block runs after the loop finishes all iterations. -> Option CQuick Check:
while-else else runs if no break [OK]
- Thinking else runs always after while
- Believing else runs only if break occurs
- Confusing else with finally block
Which of the following is the correct syntax for a while loop with an else block in Python?
?
Solution
Step 1: Recall Python while-else syntax
Theelseblock must be aligned with thewhile, not indented inside it.Step 2: Check each option's indentation and keywords
while condition: # code else: # code correctly placeselse:aligned withwhileand indents code blocks properly.Final Answer:
while condition: # code else: # code -> Option DQuick Check:
Else aligned with while, colon included [OK]
- Indenting else inside while block
- Missing colon after else
- Placing else before while
What is the output of this code?
i = 0
while i < 3:
print(i)
i += 1
else:
print('Done')Solution
Step 1: Trace the while loop iterations
Variable i starts at 0 and increments by 1 each loop until i < 3 is false. It prints 0, 1, 2.Step 2: Check else block execution
Since the loop ends normally (i becomes 3, condition false), the else block runs and prints 'Done'.Final Answer:
0 1 2 Done -> Option BQuick Check:
Loop prints 0-2, else prints Done [OK]
- Ignoring else block output
- Expecting 3 to print inside loop
- Thinking else runs only on break
Find the error in this code snippet:
i = 0
while i < 5:
if i == 3:
break
print(i)
i += 1
else:
print('Finished')Solution
Step 1: Check indentation of else block
The else block must be aligned with the while statement, but here it is not indented properly.Step 2: Verify other parts
Break is inside the loop, while condition is valid, and print uses parentheses correctly.Final Answer:
The else block is not indented properly. -> Option AQuick Check:
Else must align with while, indentation error [OK]
- Misplacing else inside loop body
- Confusing break placement
- Ignoring indentation errors
Consider this code:
n = 5
while n > 0:
if n == 3:
break
print(n)
n -= 1
else:
print('Loop completed')What will be the output and why?
Solution
Step 1: Trace loop iterations and break
n starts at 5, prints 5 and 4. When n == 3, break stops the loop immediately.Step 2: Understand else block behavior
Because the loop was stopped by break, the else block does not run, so 'Loop completed' is not printed.Final Answer:
5 4 Because break stops loop, else does not run. -> Option AQuick Check:
Break skips else, so only 5 and 4 print [OK]
- Assuming else runs even after break
- Printing 3 inside loop
- Thinking else runs before loop
