Java Programming
Connect4Techs
Jumping Statement
Jumping statements are control statements that move program execution
from one location to another.
Jump statements are used to shift program control unconditionally from one
location to another within the program. Jump statements are typically used
to forcefully end a loop or switch-case.
Types of Jumping Statement
In Java, there are three types of jumping statement:
1. Break statement
2. Continue statement
3. Return statement
➔ Break Statement :
The ‘break’ statement in Java terminates the
loop immediately, and the control of the program moves to the next
statement following the loop.
It is almost always used with decision-making statements (Java if...else
Statement).
Here is the syntax of the break statement in Java:
break;
Java Programming
FlowChart of Break statement
Example 1: Java break statement
class Topperworld {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 10; ++i) {
// if the value of i is 5 the loop terminates
if (i == 5) { break;
}
System.out.println(i);
}
}}
Output:
1
2
3
4
In the above program, we are using the for loop to print the value of i in
each iteration.
Java Programming
if (i == 5) {
break;
}
This means when the value of i is equal to 5, the loop terminates. Hence
we get the output with values less than 5 only.
➔ Continue Statement :
The ‘continue’ statement skips the
current iteration of a loop (for, while, do…while, etc).
After the continue statement, the program moves to the end of the loop.
And, test expression is evaluated (update statement is evaluated in case of
the for loop).
Here's the syntax of the continue statement.
continue;
FlowChart of continue statement
Java Programming
Example : Java continue statement
class Main {
public static void main(String[] args) {
// for loop
for (int i = 1; i <= 10; ++i) {
// if value of i is between 4 and 9
// continue is executed
if (i > 4 && i < 9) {
continue;
}
System.out.println(i);
}
}
}
Output:
1
2
3
4
9
10
In the above program, we are using the for loop to print the value of i in
each iteration.
if (i > 4 && i < 9) {
continue;
}
Here, the continue statement is executed when the value of i becomes
more than 4 and less than 9.
It then skips the print statement for those values. Hence, the output skips
the values 5, 6, 7, and 8.
Java Programming
➔ Return Statement :
The ‘return’ keyword is used to transfer
a program’s control from a method back to the one that called it. Since
the control jumps from one part of the program to another, return is
also a jump statement.
Example : Java return statement
Sum of a & b: 15