Remember
A while loop checks its condition before each iteration. Update the condition’s values so the loop can finish.
A while loop checks its condition before each iteration. Update the condition’s values so the loop can finish.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
remaining = 3
while remaining > 0:
print(remaining)
remaining = remaining - 1
print("done")Top-level scope
0A while loop checks its condition before each iteration. Update the condition’s values so the loop can finish.
(no output)
What do you expect this program to print?
CHECK CONDITION <---------+ | | +-- True --> LOOP BODY-+ | +-- False --> CONTINUE
Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
remaining = 3
while remaining > 0:
print(remaining)
remaining = remaining - 1
print("done")Top-level scope
0The example has finished. Compare the final output with the values and references above.
3 2 1 done
What do you expect this program to print?
remaining = 3
while remaining > 0:
print(remaining)
remaining = remaining - 1
print("done")The while loop in Python is a powerful control structure that lets you repeat a block of code as long as a specified condition remains True. It's commonly used when the number of iterations isn’t predetermined — making it ideal for condition-driven execution.
while condition:
# block of code
while – a keyword that starts the loop.condition – an expression that is evaluated before each loop iteration. If it is True, the loop continues; if False, it stops.: – a colon indicates the start of the indented block.True.The loop starts with i = 1. Each iteration prints i and increases it by 1. When i becomes 6, the condition i <= 5 fails and the loop exits.
i = 1
while i <= 5:
print(i)
i += 1
1
2
3
4
5
A common pitfall with while loops is forgetting to update the condition variable, which can result in an infinite loop.
# This will run forever
i = 1
while i <= 5:
print(i)
# missing i += 1
Tip: Always make sure the condition variable is being modified inside the loop.
The loop runs until the correct password is entered. Each incorrect attempt prompts the user again.
password = ""
while password != "open123":
password = input("Enter the password: ")
print("Access granted!")
Enter the password: 123
Enter the password: pass
Enter the password: open123
Access granted!
You can use the break statement to exit the loop when a specific condition is met, even if the loop condition is still True.
i = 1
while True:
print(i)
if i == 3:
break
i += 1
1
2
3
Explanation: Although the loop is set to run while True, the break statement exits it when i becomes 3.
You can use the continue statement to skip the rest of the code inside the loop for the current iteration and immediately jump to the next iteration. This is useful when you want to ignore certain values or cases without exiting the loop entirely.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
1
2
4
5
Explanation: When i == 3, continue skips the print statement and moves to the next iteration.
Connect what you noticed in the example with the idea below.
A while loop checks its condition before each iteration. Update the condition’s values so the loop can finish.
A while loop always runs its body at least once.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.