Concepts / Loop Exit Conditions

Loop Exit Conditions

break; is Java's statement for jumping out of a loop.

  • Programming
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…

When a Loop Needs a Middle Exit

Most loops are written with a stopping rule that is easy to find. A while loop normally tests whether it should continue at the beginning of each pass, and a do..while loop tests at the end. Sometimes, however, the condition that should end a loop belongs in the middle of the loop body. Some work has to happen first, such as reading a value, and only then can the program decide whether the loop is finished.

Java provides the break statement for this situation. Written as break;, it jumps out of the loop that contains it. This article follows one value through a loop to show exactly what break changes, where the program goes next, and how to decide where the exit check should sit.

Definition

A loop exit condition is the condition under which a loop stops executing. The exit condition can be tested at the beginning of a loop, at the end of a loop, or in the middle of a loop. The break statement supplies the middle-of-loop option.

Tracing a Loop That Accepts Input

The source describes a loop that begins by accepting input, checks in the middle whether the input is positive, and uses break when the input is acceptable. The following example is an original version of that pattern. Read it once to see its structure, then trace it step by step.

java
Output
Suppose the user types -5, then 3.
Please enter a positive number.
Accepted: 3
Notes on the code
  • while (true) keeps the loop running until something inside it leaves the loop
  • The input is read first, before any check happens
  • The if statement is the middle-of-loop check that decides whether break runs
  • The final println runs only after the loop has been exited

What do you think happens?

When the value read is 3, so that n > 0 is true, what happens next? And when the value is -5, so that n > 0 is false, what happens?

Reveal answer
Answer:

When n > 0 is true, break runs, the loop ends immediately, and the program continues with the println statement after the loop. When n > 0 is false, break does not run, the message is printed, and the loop returns to the top to read another value.

The test in the middle of the body decides which of two paths is taken. The true path leaves the loop. The false path stays in the loop and repeats it.

What break Does to Control Flow

When break executes, the loop ends immediately. The program does not finish the rest of the loop body, and it does not go back to the loop's test. Instead, control moves to the statement that follows the loop. In the example above, that statement is the final println, which prints the accepted value.

This is different from the normal way a loop ends. When a while loop's test at the beginning becomes false, the loop stops because its condition no longer holds, and control also moves to the statement after the loop. The difference is where the decision is made. With break, the decision is made inside the body, at the point where the program already knows what it needs to know.

Read inputn = in.nextInt()n > 0 ?middle-of-loop testbreakleave the loop nowStatement after loopprintln AcceptedPrint messageskipped when break runstruefalserepeat loopcontrol moves here
After the middle check inside the loop body is true and break runs, which statement executes next, and which statements in the body are skipped?

Tracing Two Passes Through the Loop

Tracing means following the values and the path of control one step at a time. Use the input -5 followed by 3. On the first pass, n becomes -5. The test n > 0 is false, so break does not run. The message prints and the loop returns to the top. On the second pass, n becomes 3. The test n > 0 is true, so break runs. The loop ends at that point, and the program moves to the statement after the loop without printing the message a second time.

Worked example

Tracing the input loop with -5 then 3

Trace the program when the user enters -5 and then 3. State the value of n, the result of the test, and where control goes after each step.

  1. 1
    Pass 1: read input

    n is set to -5.

  2. 2
    Pass 1: middle check

    n > 0 is false, so the if body is skipped and break does not run.

  3. 3
    Pass 1: continue loop

    The message is printed, and control returns to the top of the loop to read again.

  4. 4
    Pass 2: read input

    n is set to 3.

  5. 5
    Pass 2: middle check

    n > 0 is true, so break runs.

  6. 6
    Exit

    The loop ends immediately. Control moves to the println after the loop, which prints the accepted value.

Result

The output is one rejection message followed by Accepted: 3. The loop body did not run past the break on the second pass.

Pass 1input -5n > 0 falseno breakPass 2input 3n > 0 truebreak runsAfter loopprintln Acceptedloop againbreak
How does a break in the middle of the loop body end the loop before the loop's normal repetition happens again?

Choosing Where the Exit Check Belongs

The position of break determines which statements run on the final pass. If the check is placed at the very start of the body, the statements that follow it run only on passes where the check fails. If the check is placed after some work, that work always runs first, which is what the input example needs: the value must be read before it can be tested. Decide where the exit check belongs by asking which information the loop needs before it can decide to stop.

Where the exit check sitsWhat runs before the checkTypical situation
Beginning of the loop (while)Nothing from the current passThe stopping rule depends only on values already known
Middle of the loop (break)The work needed to produce the value being testedThe value must be read or computed before it can be judged
End of the loop (do..while)The entire body of the current passThe body must run at least once before the test

Three possible locations for a loop's exit test, as described in the source material

Mistakes With break Placement

  • Common mistake

    Assuming break sends control back to the loop test

    What it looks like

    Expecting the program to re-check n > 0 right after break runs in the input example.

    Why it fails

    When break executes, the loop ends immediately. The program does not return to the loop's test or finish the rest of the body.

    Do this instead

    After break, control moves to the statement that follows the loop, which here is the println that prints the accepted value.

  • Common mistake

    Placing break before the work that produces the tested value

    What it looks like

    Writing the if (n > 0) check before the line that reads n, so the test uses a stale or uninitialized value.

    Why it fails

    The exit condition can only be tested once the value it depends on exists.

    Do this instead

    Put the read or computation first, then the check, then break.

  • Common mistake

    Forgetting that the statements after the check are skipped on the exit pass

    What it looks like

    Expecting the rejection message to print on the pass where n is 3.

    Why it fails

    On the pass where break runs, the statements after the if are not reached.

    Do this instead

    Trace the final pass to confirm which lines run before and after the break.

Practice Tracing a Mid-Loop Exit

EASY

Using the input loop from this article, trace the program when the user enters 0, then -2, then 7. Write down the value of n on each pass, whether break runs, and the output printed. Then state which statement runs immediately after the loop ends.

Hints
  • Check n > 0 on each pass, starting with 0.
  • Remember that break runs only when the test is true.
  • The rejection message prints on every pass where break does not run.

Key Takeaways

  • The break statement in Java ends the loop it appears in immediately.
  • When break executes, control moves to the statement that follows the loop, not back to the loop's test.
  • A break condition can be placed in the middle of a loop, which is useful when a value must be read or computed before the stopping rule can be checked.
  • Tracing each pass shows which statements run before the loop ends, so the position of break should be chosen to match the work the loop needs to do.