Jump Statements
Jump statements are used to control the flow of program
and transfer the flow of program one point to another point.
- break statement
- continue statement
- return
- break statement :
The break statement is used to terminate the current loop or switch statement and transfer control to another point of the program. It's often used to exit a loop prematurely when a certain condition is met.
# Example :
public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { break; } System.out.println(i); } }Output // 1, 2, 3, 4
2. continue statement :
The continue statement is used to skip the rest of the current iteration and move to the next iteration of a loop.
for (int i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.println(i); }
- break statement :
The break statement is used to terminate the current loop or switch statement and transfer control to another point of the program. It's often used to exit a loop prematurely when a certain condition is met.
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
Output // 1, 2, 3, 4
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(i);
}
Output // 1, 2, 3, 4
3. return :
The return statement is used to exit a method and transfers control back to the caller along with the specified value.
public int add(int a, int b) {
int sum = a + b;
return sum;
}
Comments
Post a Comment