Jump Statements
In Java, we use jump statements to transfer the control of the program to other parts of the program. There are various types of jump statements in java.
i) break statement: The loops are terminated using the Java break expression. It can be used within a loop. When a break statement is found within a loop, the loop is ended and control is passed to the next statement after the loop. There are three uses for the Java break statement.
- It can be used to terminate a statement sequence in the switch statement.
- It can be used to terminate a loop.
- It can be used as a civilized form of goto statement for performing a labeled break.
Syntax:
break;
Example:
public class JavaBreakStatement {
public static void main(String args[]) {
for(int i = 1; i <= 10; i++) {
// terminate the loop if i is 5.
if(i == 5)
break;
System.out.print(i + “ “);
}
}
}
Output:
1 2 3 4
ii) labeled break statement: Java labeled break statement can be used as a civilized form of a goto statement. Java doesn’t provide a goto statement. It utilizes a label, which is a code block that must enclose the break statement but does not have to do so immediately. To exit a series of nested blocks, we can use labeled break statements.
Example:
public class JavaLabeledBreak {
public static void main(String args[]) {
boolean b = true;
// first label
first: {
// second label
second: {
// third label
third: {
System.out.println(“Before the break statement”);
// break will take the control out
// of the second label
if(b)
break second;
System.out.println(“This would not be execute”);
}
System.out.println(“This would not be execute”);
}
System.out.println(“This is after the second block”);
}
}
}
Output:
Before the break statement
This is after the second block
iii) continue statement: Java continue statement is used to skip the current iteration of the loop. We can use continue statements in all types of loops. In the case of for loop, the continue statement forces the control to jump immediately to the update statement. In case of while and do-while loop the control immediately jumps to the boolean expression.
Syntax:
continue;
Example:
public class JavaContinueStatement {
public static void main(String args[]) {
for(int i = 1; i <= 5; i++) {
// using continue statement
// skip the value when i is 3
if(i == 3)
continue;
// Printing the value
System.out.print(i + “ “);
}
}
}
Output:
1 2 4 5
iv) Continue Statement with nested loop: It continues the inner loop only when you use the continue statement inside the inner loop.
Example:
public class JavaContinueNested {
public static void main(String args[]) {
// outer for loop
for(int i = 1; i <= 3; i++) {
// inner for loop
for(int j = 1; j <= 3; j++) {
// continue statement inside the inner loop
// to skip the current iteration when
// i == 2 and j == 2
if(i == 2 && j == 2) {
continue;
}
System.out.println(i + “ “ + j);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
v) continue statement with labeled for loop: Like the java break statement, the continue statement may also specify a label to describe which enclosing loop to continue. Let’s look at the example to understand better.
Example:
public class JavaLabeledContinue {
public static void main(String args[]) {
aa:
for(int i = 1; i <= 3; i++) {
bb:
for(int j = 1; j <= 3; j++) {
if(i == 2 && j == 2) {
// using continue statement with label
continue aa;
}
System.out.println(i + “ “ + j);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
vi) return statement: Java return statement is used to explicitly return from the method. It transfers the program control back to the caller method. The return statement immediately terminates the method in which it is executed. We need to declare a method return type in its method declaration. Any method declared void doesn’t return any value. A compile-time error can occur if you attempt to return a value from a method that is declared void.
Syntax:
return;
Example:
public class JavaReturnStatement {
// function to find sum
public static int findSum(int num1, int num2) {
int sum = num1 + num2;
// return sum
return sum;
}
public static void main(String args[]) {
// Creating object of class
JavaReturnStatement obj = new JavaReturnStatement();
// Function call
System.out.println(obj.findSum(10, 20);
}
}
Output:
30