A while loop needs a progress story
Identify the initial state, condition and update. In this model, position increases by one on each move. If the update is missing, the condition may remain true forever.
Week 05 / Describe and decide
Repeat an action until an outcome is known.
A for loop worked when we chose the observation times in advance. A reference search ends when a switch is found, so the number of moves is not known beforehand.
A simulated carriage searches for its reference switch. It may make at most five moves; the switch is checked before moving and after each move.
Start with the problem
Act out two searches on paper: the switch is already active, and the switch never activates. How many moves should each make, and what message should each return?
A while loop continues while a condition holds. A counter limits the attempts, and an explicit result separates finding the reference from running out of moves.
By the end: Trace the search without an extra move, including when the switch activates on the fifth move.
Identify the initial state, condition and update. In this model, position increases by one on each move. If the update is missing, the condition may remain true forever.
A bounded loop can stop because it found the reference or because it ran out of moves. The exit alone is not success. Inspect the final state before choosing the outcome message.
A switch reached on move five counts as found. Reporting timeout immediately after the fifth move without checking the switch would create an off-by-one error. A real homing design needs hardware-specific limits and supervision beyond this teaching model.
Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.
position = 0
reference_at = 5
max_moves = 5
while position < reference_at and position < max_moves:
position = position + 1
print('Move', position)
if position >= reference_at:
print('REFERENCE FOUND')
else:
print('SEARCH LIMIT REACHED')Move 1 Move 2 Move 3 Move 4 Move 5 REFERENCE FOUND
A while loop checks the current state before each iteration. Its condition says when to continue; an update must move the state toward stopping. A correct condition alone cannot guarantee termination.
Draw a cycle: test attempts < 3 → perform one attempt → increase attempts → test again. Mark the exit when the test is false.
Predict before running. Starting with attempts = 0, how many attempts occur? What if continue runs before the increment every time?
There are three attempts in the first version. Every path that continues must make the required progress, or another stopping condition must apply. break exits immediately; continue skips the rest of the current iteration.
Change one thing. Start at attempts = 3 and predict whether the body runs. Distinguish zero iterations from running once and then checking.
Türkçe: Koşul devam etmeyi söyler; güncelleme bitişe ilerlemeyi sağlar. continue güncellemeyi atlarsa döngü sonsuza gidebilir.
Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.
Switch becomes active at position 3; limit 5 moves
Question: How many moves occur, and why does the loop stop?
The success condition ended the search before the budget was exhausted.
Switch becomes active at position 5; limit 5 moves
Question: Is the fifth move success or exhaustion?
The final check prevents a boundary success from being mislabeled as failure.
Switch would activate at position 8; limit 5 moves
Question: What should be reported after the fifth move?
Reaching a software limit does not establish where the physical reference is. Do not silently report success.
Interactive walkthroughs of while Loops, break & continue. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.
Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.
Something to take away: A trace for early success, boundary success and exhaustion, plus explicit messages for each outcome.
Read the notebook's teaching cells before these exercises.
Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.
Notes stay in this browser. Download a copy to keep them.
Ask AI to implement the bounded simulator. Challenge it with an already-active switch and a switch reached on the last allowed move. Look for code that equates every loop exit with success.
You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.