Concepts / Conditional Statements (if/else)

Conditional Statements (if/else)

For abc, identify that the condition is false and follow the remaining loop body to the sufficient-length message.

  • Programming
Interactive lab

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

  1. Python evaluates the if condition first.
  2. If it is False, it tries each elif in order; the first True one runs and the rest are skipped without being tested.
  3. If none is True, the else branch runs.
  4. 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.

Educational simulation

Loading the simulation…

Interactive lab

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

  1. Evaluate the condition. If it is False, skip the body and continue after the loop.
  2. If it is True, run the body from the top, then go back and evaluate the condition again.
  3. continue skips the rest of the body and goes straight back to the condition check.
  4. 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').
  5. 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.

Educational simulation

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.

Key point

Continue skips the rest of the current loop iteration. It does not exit the loop entirely.

Iteration startsConditioncontinueNext iterationRemaining loop bodyevaluatecondition is trueskip remaining bodycondition is falseiteration ends
When the loop reaches continue, which statements are skipped, where does control flow next, and what output is produced?

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.

Worked example

Checking input length

A loop accepts user input and should process only strings that are at least 3 characters long.

  1. 1
    Input 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.

  2. 2
    Input 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.

  3. 3
    Input abc

    This input is at least 3 characters long. The condition that leads to continue is false, so continue is not executed.

  4. 4
    Processing

    Because continue was not executed for abc, the loop reaches the final processing statement and prints the sufficient-length message.

Result

Short inputs are skipped, while abc reaches the processing statement. The loop continues to ask for input after each iteration.

User inputLength below 3continueProcess inputTop of loopcheck lengthtrueask againfalseiteration ends
How does the condition determine whether execution follows the continue branch or the remaining loop-body branch?

Continue Compared with Break

StatementEffect on current iterationEffect on loop
continueStops executing the current iteration immediatelyThe loop continues with its next iteration
breakStops executing the current iteration immediatelyThe 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.

continuebreakNext iterationLoop exitsreturn to loopleave loop
How does execution proceed differently after continue versus break, and does the loop run another iteration or end immediately?

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.

Before

Input a or 12: condition is true, so continue is executed. The final processing statement is not reached.

Short input is skipped

After

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?

  • The loop exits immediately

  • The processing statement is skipped

  • The processing statement runs

Reveal answer
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 like

    A short input reaches continue, and the learner assumes no later input can be examined.

    Why it fails

    Continue skips only the current iteration and returns to the top of the loop.

    Do this instead

    Trace the next iteration after every continue.

  • Common mistake

    Counting statements after continue as executed

    What it looks like

    A learner includes the final processing message in the output for a or 12.

    Why it fails

    Code between continue and the end of the loop block is skipped for that iteration.

    Do this instead

    Stop tracing the current iteration as soon as continue is reached.

  • Common mistake

    Confusing continue with break

    What it looks like

    A learner predicts that the loop exits after an input triggers continue.

    Why it fails

    Break exits the loop entirely, while continue begins the next iteration.

    Do this instead

    Ask whether the control flow returns to the loop or leaves it.

Best practice

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

MEDIUM

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.
  1. 1
    Continue immediately stops the current loop iteration.
  2. 2
    Statements after continue in the same loop body are skipped for that iteration.
  3. 3
    After continue, control returns to the top of the loop for the next iteration.
  4. 4
    Break exits the loop entirely, unlike continue.
  5. 5
    Continue 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.

Related concepts

More concepts starting with C