Conditional Statements (if/else)
For abc, identify that the condition is false and follow the remaining loop body to the sufficient-length message.
Try it: if / elif / else
How Python tests if / elif conditions from the top, runs only the first true branch, and how and / or stop evaluating as soon as the answer is known (short-circuiting).
How it works
- Python evaluates the if condition first.
- If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
- If none is True, the else branch runs.
- and stops at the first False, or stops at the first True — the right side may never be evaluated.
Default run (5 steps): score = 84; A = 90, B = 80, C = 70. Python tests the conditions from the top. … print(grade) shows B.
Loading the simulation…
Try it: while Loops, break and continue
How a while loop re-evaluates its condition before every pass, how break leaves the loop at once, how continue jumps back to the condition, how a sentinel value ends a while True loop, and why a loop whose condition never becomes False runs forever.
How it works
- Evaluate the condition. If it is False, skip the body and continue after the loop.
- If it is True, run the body from the top, then go back and evaluate the condition again.
- continue skips the rest of the body and goes straight back to the condition check.
- break leaves the loop immediately; the condition is not checked again (while True loops rely on it, e.g. stopping at a sentinel like 'done').
- If nothing in the body can make the condition False, the loop never ends; the lab's guard stops it after N iterations and says so.
Default run (21 steps): Ready: Countdown. A while loop checks its condition before every pass. … Finished after 5 iterations and 6 condition checks.
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. input() returns the lines you type in the list, one per call (the prompt is not printed), and raises EOFError when they run out, like a closed keyboard stream. The guard is the lab's, not Python's.
Loading the simulation…
A Loop Can Skip Without Stopping
A loop does not always need to process every iteration in the same way. Sometimes an input should be ignored for the current iteration, while the loop itself should continue running. Python provides the continue statement for this situation. When Python reaches continue, it immediately stops executing the current iteration and returns to the top of the loop to begin the next iteration.
Continue skips the rest of the current loop iteration. It does not exit the loop entirely.
Tracing the Skipped Work
The important detail is the position of continue inside the loop body. Any code between continue and the end of that loop block is skipped for the current iteration. Control then returns to the top of the loop. The next iteration begins, rather than continuing downward through the remaining statements from the skipped iteration.
Checking input length
A loop accepts user input and should process only strings that are at least 3 characters long.
- 1Input a
The input has fewer than 3 characters. The continue statement is executed, so the processing statement at the end of the loop is skipped.
- 2Input 12
This input also has fewer than 3 characters. The continue statement again skips the processing statement and returns control to the top of the loop.
- 3Input abc
This input is at least 3 characters long. The condition that leads to continue is false, so continue is not executed.
- 4Processing
Because continue was not executed for abc, the loop reaches the final processing statement and prints the sufficient-length message.
Short inputs are skipped, while abc reaches the processing statement. The loop continues to ask for input after each iteration.
Continue Compared with Break
| Statement | Effect on current iteration | Effect on loop |
|---|---|---|
| continue | Stops executing the current iteration immediately | The loop continues with its next iteration |
| break | Stops executing the current iteration immediately | The loop exits entirely |
Both statements alter loop execution, but only continue returns control to the next iteration.
The difference is what happens after the current iteration stops. Continue returns to the top of the loop, so later iterations can still occur. Break exits the loop entirely, so no further loop iterations occur. Choose continue when an individual item should be skipped but the loop should keep examining later items. Choose break when the loop should end.
Using Continue as a Filter
A useful pattern is to place a condition before the main processing statement. If the input does not meet the minimum requirement, continue filters it out of the current iteration. If the input meets the requirement, execution moves past the condition and reaches the processing statement.
Input a or 12: condition is true, so continue is executed. The final processing statement is not reached.
Short input is skipped
Input abc: condition is false, so continue is not executed. The final processing statement is reached.
Sufficient input is processed
Trace each iteration in order. For a, identify the true length condition, the continue statement, and the skipped processing. Repeat the same reasoning for 12. For abc, identify that the condition is false and follow the remaining loop body to the sufficient-length message.
What do you think happens?
What happens when the input is abc in the minimum-length example?
Reveal answer
The processing statement runs
The continue condition is based on input length below 3. For abc, that condition is false, so continue is not executed and the final processing statement is reached.
Mistakes in Loop Tracing
Common mistake Assuming continue ends the loop
What it looks likeA short input reaches continue, and the learner assumes no later input can be examined.
Why it failsContinue skips only the current iteration and returns to the top of the loop.
Do this insteadTrace the next iteration after every continue.
Common mistake Counting statements after continue as executed
What it looks likeA learner includes the final processing message in the output for a or 12.
Why it failsCode between continue and the end of the loop block is skipped for that iteration.
Do this insteadStop tracing the current iteration as soon as continue is reached.
Common mistake Confusing continue with break
What it looks likeA learner predicts that the loop exits after an input triggers continue.
Why it failsBreak exits the loop entirely, while continue begins the next iteration.
Do this insteadAsk whether the control flow returns to the loop or leaves it.
When tracing a loop with continue, record three facts for every iteration: the input or value being examined, whether the condition leading to continue is true, and whether the remaining loop-body statements execute.
Trace It Yourself
For the input sequence hi, hello, x, and stop, trace what happens at each step. Identify which print statements execute, which iterations are skipped, and why.
Hints
- For each input, first determine whether the condition leading to continue is true.
- If continue executes, stop tracing that iteration immediately.
- Do not assume that continue exits the loop; distinguish it from break.
- 1Continue immediately stops the current loop iteration.
- 2Statements after continue in the same loop body are skipped for that iteration.
- 3After continue, control returns to the top of the loop for the next iteration.
- 4Break exits the loop entirely, unlike continue.
- 5Continue can filter out inputs that do not meet a requirement while allowing later iterations to be processed.
Key Takeaways
- Continue skips the remainder of one loop iteration and starts the next iteration.
- A condition can use continue to filter inputs before the main processing statements.
- Statements after continue do not execute during the iteration that reached continue.
- Break ends the loop entirely, whereas continue allows later iterations to run.
- To trace a loop correctly, stop the current iteration at continue and resume at the loop's top.