Jump Statements in JAVA - Types of Statements in JAVA (With Examples)
Jump statements in Java are used to transfer control from one part of a program to another, altering the normal sequential flow of execution. Java provides three main jump statements: break, continue, and return.
break Statement
Immediately terminates the nearest enclosing loop or switch statement, and execution resumes right after it.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
// prints 1 2 3 4
continue Statement
Skips the rest of the current loop iteration and moves directly to the next iteration, without exiting the loop entirely.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
// prints 1 2 4 5
return Statement
Exits the current method immediately and optionally sends a value back to the caller. Any code after return in the same block will not execute.
int square(int n) {
return n * n; // exits the method with a value
}
Labeled break and continue
Java also supports labels on loops, allowing break or continue to target a specific outer loop when working with nested loops.
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + "," + j);
}
}
break— exits a loop or switch entirelycontinue— skips to the next loop iterationreturn— exits a method, optionally returning a value
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
Complete Examples You Can Compile
The program below shows break, continue and a labeled break in full, along with the output each one produces.
public class JumpDemo {
public static void main(String[] args) {
// break: stop at the first number divisible by 7
for (int i = 1; i <= 20; i++) {
if (i % 7 == 0) {
System.out.println("First multiple of 7: " + i); // 7
break;
}
}
// continue: print only odd numbers
for (int i = 1; i <= 9; i++) {
if (i % 2 == 0) {
continue;
}
System.out.print(i + " "); // 1 3 5 7 9
}
System.out.println();
// labeled break: search a 2D grid and leave both loops
int[][] grid = { {4, 8, 1}, {9, 3, 6}, {2, 5, 7} };
int target = 3;
search:
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
if (grid[r][c] == target) {
System.out.println("Found at row " + r + ", col " + c); // row 1, col 1
break search;
}
}
}
}
}
Without the label, break would only leave the inner loop, and the outer loop would keep scanning the remaining rows for no reason.
break Inside switch, and How Arrow Syntax Changes Things
In a classic switch, forgetting break lets execution "fall through" into the next case. This is a common source of bugs:
int day = 2;
switch (day) {
case 1: System.out.println("Monday");
case 2: System.out.println("Tuesday"); // runs
case 3: System.out.println("Wednesday"); // also runs - no break above!
break;
default: System.out.println("Other day");
}
// Arrow-style switch (Java 14 and later) never falls through
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Other day";
};
The arrow form also lets a switch return a value. Inside a block in a switch expression, the keyword yield hands back the result instead of return.
Early Exit With return, and Picking the Right Jump
A return at the top of a method can reject bad input before the main logic, which keeps code flat and readable. In a void method, a bare return; simply ends the method.
static double average(int[] marks) {
if (marks == null || marks.length == 0) {
return 0.0; // guard clause
}
int total = 0;
for (int m : marks) total += m;
return (double) total / marks.length;
}
Be aware that a finally block still runs after a return inside try. Avoid returning from finally itself, because it hides exceptions and overrides the earlier result.
Which one should you reach for?
Each jump statement answers a different question. Ask yourself what should happen next, and the choice usually becomes obvious.
- "I found what I need, stop looking." Use
break, orreturnif the search lives in its own method. - "This item is not interesting, move to the next one." Use
continue. It often replaces a large if block wrapped around the rest of the loop body. - "The whole method's job is done or impossible." Use
return. - "I need to stop an outer loop from deep inside." A labeled
breakworks, but extracting the nested loops into a method that returns is often cleaner.
For simple filtering or searching over collections, Java streams with methods such as filter and findFirst can remove the need for explicit jumps altogether.
Errors Beginners Make With Jump Statements
- Unreachable code. Any statement directly after
break,continueorreturnin the same block causes a compile error. - Infinite while loops. If
continueappears before the counter update in awhileloop, the counter never changes. Update first, or use aforloop. - Expecting break to leave an if.
breakapplies to loops and switch only, not to plain if blocks. - Looking for goto. Java reserves the word
gotobut does not support it; labeled break and continue cover the valid uses. - Too many exit points. If a loop has several breaks, consider moving it into a method that returns the result instead.