Trace with `break`
The `break` statement exits a loop immediately. Predict the exact output of this code, including all printed lines.
while i < 10:
print("Current i:", i) if i == 3:
print("Breaking!") break i = i + 1 print("Loop finished.")
Skip with `continue`
Use a `while` loop and the `continue` statement to print only the *even* numbers between 1 and 7 (inclusive). `continue` skips the rest of the current iteration.
Advanced Missions
Tackle these challenges involving nested loops and the `while True`/`break` pattern. Pay attention to indentation!
Mission 1: Nested Pattern
Use *nested* `while` loops to print a small triangle pattern of stars (`*`). The output should be:*
**
***
Mission 2: `while True` Stop
Simulate reading numbers until the number `0` is encountered. Use a `while True:` loop and a `break` statement. Print the sum of all numbers entered *before* 0. (Simulate input with a list `[5, 8, 3, 0, 4]`).
Debugging Challenge
This code is supposed to print numbers from 10 down to 1, but it has an infinite loop! Find the bug and fix it in the editor below.
Mission: Fix the Countdown
The variable `count` is never changed inside the loop, causing the condition `count > 0` to always be true. Add the line to decrease `count`.
while count > 0:
print(count)
# Bug: Missing update! count never decreases!
Advanced Practice Complete!
Fantastic job navigating these advanced `while` loop concepts! You're building solid looping skills.
Review the standard practice, watch the video, or head back to the course menu.