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.