What Are Jump Statements in Java?
Normally a loop runs its body from top to bottom on every pass and stops only when its condition becomes false. Jump statements let you change that flow from inside the body:
| Statement | Effect | Lesson |
|---|---|---|
break | Leave the loop (or switch) now | This lesson |
continue | Skip to the next pass | This lesson |
return | Leave the whole method | Methods lessons |
Both statements are written on their own, followed by a semicolon:
They’re almost always placed inside an if, because jumping on every pass is rarely useful.
What Does break Do in Java?
break immediately exits the nearest enclosing loop. The loop’s condition is not checked again and a for loop’s update does not run. Execution jumps to the first statement after the loop.
Example 1: Find the First Failing Score
Code (Java 21):
Output:
Checking score 65Checking score 72
Checking score 38
First failing score 38 at index 2
The loop never checked 90 or 25. Once the answer was found, break stopped the extra work. The failIndex = -1 default lets you tell “found” apart from “loop ran to the end”.
What Does continue Do in Java?
continue skips the remaining statements in the current pass and starts the next one. The loop itself keeps running.
Example 2: Skip Missing Sensor Readings
Code (Java 21):
Output:
Day 1: 12 mmDay 2: no reading, skipped
Day 3: 8 mm
Day 4: no reading, skipped
Day 5: 5 mm
Total = 25 mm over 3 days
Dry Run of Example 2
day | rainfall[day] | == -1? | Action | total | validDays |
|---|---|---|---|---|---|
| 0 | 12 | no | add, print | 12 | 1 |
| 1 | -1 | yes | print “skipped”, continue → day++ | 12 | 1 |
| 2 | 8 | no | add, print | 20 | 2 |
| 3 | -1 | yes | print “skipped”, continue → day++ | 20 | 2 |
| 4 | 5 | no | add, print | 25 | 3 |
| 5 | — | loop ends (5 < 5 is false) | print total | 25 | 3 |
On the continue passes, the total and validDays lines were skipped, but day++ still ran. That’s why the for loop moved on safely.
How break and continue Work: Flowchart
break jumps straight out of the loop. continue skips the “Rest of body” box, but in a for loop it still passes through the update before the condition is checked again.
Copy Mermaid Code
Same loop, same condition, one keyword changed:
Code (Java 21):
Output:
break: 1 2 3 continue: 1 2 3 5 6| Feature | break | continue |
|---|---|---|
| What it ends | The whole loop | Only the current pass |
| Where execution goes | First statement after the loop | Next pass (update in for, condition in while/do-while) |
Works in switch | Yes: exits the switch | No, unless the switch is inside a loop (then it continues the loop) |
| Typical use | Stop after finding a result; stop on bad input | Skip invalid, empty, or unwanted items |
| Infinite-loop risk | Low | High in while and do-while if it skips the update |
Where Does continue Jump in Each Loop?
| Loop | After continue, Java goes to… |
|---|---|
for | The update (i++), then the condition |
while | The condition at the top. No automatic update. |
do-while | The condition at the bottom. No automatic update. |
break and continue in a While Loop
A common real pattern: read lines, skip blank ones with continue, and stop at a command with break.
Code (Java 21):
Input lines: list files, (blank), (spaces only), save, quit, delete all
Output:
Command 1: LIST FILESCommand 2: SAVE
Processed 2 commands
The two blank lines were skipped, and delete all was never processed, because break ended the loop at quit. This continue is safe because in.nextLine() already advanced the input before the continue ran.
The continue Trap in While Loops
If continue runs before the line that moves the loop forward, the same value is checked forever. You met this in the While Loop in Java lesson. The rule: in while and do-while loops, update the state before any continue. A for loop is immune, because its update always runs.
continue in a Do-While Loop
Code (Java 21):
Output:
1 2 4 5 7 8 10Multiples of 3 were skipped. continue jumped to while (n < 10) at the bottom, and since n++ comes first in the body, the loop always makes progress.
break in a Nested Loop in Java
A plain break exits only the innermost loop that contains it. The outer loop carries on.
Code (Java 21):
Output:
* <- row 1 done* * <- row 2 done
* * * <- row 3 done
Outer loop finished
The inner loop was cut short on every row, but all three rows still ran. A plain continue also affects only the innermost loop. To exit or continue an outer loop directly, Java has labeled break and labeled continue, which are covered in the next lesson, Labeled Statements in Java.
break in a Switch Statement in Java
Inside a switch, break ends the switch so execution doesn’t “fall through” into the next case. When that switch sits inside a loop, the break exits only the switch; the loop keeps running.
Code (Java 21):
Output:
After L: position = -1After R: position = 0
Unknown move X, ignored
After X: position = 0
After R: position = 1
Final position: 1
All four moves were processed; no break left the for loop. You’ll study switch, fall-through, and the newer arrow syntax in Switch Statement in Java.
break vs return in Java
break leaves the loop; return leaves the whole method, skipping any code after the loop.
Code (Java 21):
Output:
withBreak():
break loop: 1
after loop (break) - still runs
withReturn():
return loop: 1back in main
(The braceless if (i == 2) break; keeps the comparison compact. In your own code, prefer braces.)
Using continue as a Guard Clause
When several conditions should each skip an item, continue keeps the body flat instead of deeply nested.
Code (Java 21):
Output:
Sending newsletter to asha@mail.comSending newsletter to meena@mail.comEach guard rejects one kind of bad input. Without continue, the println would sit inside three nested if blocks. For a single simple check, though, a plain if (…) { … } is just as clear.
Common Mistakes With break and continue
Mistake 1: Using break Outside a Loop or Switch
Compiler output (Java 21):
BreakOutside.java:5: error: break outside switch or loopbreak;
^
1 error
break doesn’t exit an if. To stop a method early, use return.
Mistake 2: Using continue in a Switch That Isn’t in a Loop
Compiler output (Java 21):
ContinueOutside.java:6: error: continue outside of loopcontinue;
^
1 error
continue only makes sense inside a loop. In a switch, use break.
Mistake 3: Code After break in the Same Block
Compiler output (Java 21):
AfterBreak.java:5: error: unreachable statementSystem.out.println(i);
^
1 error
A statement directly after an unconditional break or continue can never run, so Java refuses to compile it. Put the jump inside an if.
Mistake 4: continue Skips the Update in While or Do-While
If i++ comes after continue, the loop hangs. Move the update above the continue, or switch to a for loop when you’re walking an index.
Mistake 5: Expecting break to Exit All Nested Loops
A plain break leaves only the inner loop, as Example 6 shows. Use a labeled break, a flag checked by the outer loop, or move the loops into a method and return.
Mistake 6: Too Many Jumps in One Loop
A body with several continue guards, a mid-body break, and nested ifs is hard to follow. If a loop has more than two jumps or doesn’t fit on a screen, move part of the work into a well-named method.
Best Practices
- Use
breakwhen the result is found or going further is pointless (search, validation). - Use
continuefor clear “skip this item” guards placed at the top of the body. - In while and do-while loops, update the loop state before any
continue. - Record why the loop ended (a found index, a flag) so the code after the loop can tell.
- Prefer a clear loop condition over a
while (true)full of jumps when the stopping rule is simple. - Keep jumps few and visible; refactor into a method when they multiply.
- Always use braces around the
ifthat triggers a jump.
Real-World Example: Shopping Within a Budget
This program uses both statements: continue skips out-of-stock items, and break stops once the next item would exceed the budget.
Code (Java 21):
Output:
Notebook: added, spent Rs 120Pen: out of stock, skipped
Calculator: added, spent Rs 770
Bag: Rs 900 exceeds budget, stopping
Remaining budget: Rs 230
Design choice: break means “stop shopping at the first item you can’t afford”. If you’d rather keep looking for cheaper items later in the list, change that break to continue. That one-word change is the whole difference between the two statements.
break in DSA: Checking for a Prime Number
Stopping at the first divisor turns a long scan into a short one.
Code (Java 21):
Output:
29 is prime (4 divisions)91 is not prime (6 divisions)
2 is prime (0 divisions)
1 is not prime (0 divisions)
For 91, the loop stopped at 7 (91 = 7 × 13) instead of trying every divisor. The d * d <= n condition keeps the work at O(√n).
Other common uses: searching a list for the first match, stopping a file reader at an end marker, skipping comment lines (#…) when parsing config files, and leaving a while (true) server or menu loop.
Practice Problems
- Easy: Print numbers 1 to 20 but skip multiples of 5. Hint:
continuewheni % 5 == 0. - Easy: Find the first negative number in an array and print its index, or “none”. Hint:
breakplus a-1default. - Easy–Medium: Sum numbers from an array, skipping zeros, and stop entirely if a number greater than 100 appears. Hint: one
continue, onebreak. - Medium: Print all prime numbers between 1 and 50 using a nested loop with
break. Hint: reuse the prime check as the inner loop. - Medium: Count the vowels in a sentence, skipping spaces and digits with
continue. Hint:Character.isLetter(ch).
Try them in the ExamAdda online compiler.
Interview Questions on break and continue in Java
1. What is the difference between break and continue?
break ends the entire loop; continue ends only the current pass and moves to the next one.
2. Does continue skip the update in a for loop?
No. After continue, a for loop still runs its update (e.g., i++) before checking the condition.
3. Why can continue cause an infinite loop in a while loop?
A while loop has no automatic update, so if continue runs before the statement that changes the loop variable, the same check repeats forever.
4. In nested loops, which loop does break exit?
Only the innermost loop that contains it. To exit an outer loop, use a labeled break (next lesson), a flag, or return.
5. What does break do inside a switch that is inside a loop?
It exits only the switch; the loop continues with its next pass.
6. Can you use break inside an if statement that isn’t in a loop?
No. It fails with “break outside switch or loop”. Use return to leave a method early.
7. What’s the difference between break and return?
break exits the loop and continues with the rest of the method; return exits the method entirely.
8. Output of for (int i = 0; i < 5; i++) { if (i % 2 == 0) continue; System.out.print(i); }?
13. The even values 0, 2, and 4 are skipped.
Article Summary
breakexits the nearest enclosing loop or switch;continueskips to the next pass of the nearest loop.- After
continue, a for loop runs its update; while and do-while jump straight to the condition, so update first. - In nested loops, plain
breakandcontinueaffect only the innermost loop; labeled forms target outer loops. - Inside a switch within a loop,
breakexits only the switch;returnleaves the whole method. - Use both sparingly: they should make a loop clearer, not hide its logic.
Conclusion
break and continue give you fine control over a loop: stop as soon as you have an answer, or skip what doesn’t matter. Before adding one, ask where execution will go next, and in while and do-while loops make sure the loop still moves forward. Next, learn how to jump out of an outer loop in one step in Labeled Statements in Java, and practise with ExamAdda practice questions.
Previous lesson: Do-While Loop in Java
Next lesson: Labeled Statements in Java