In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly. Java supports three jump statements: break, continue, and return.
We’ll be covering the following topics in this tutorial:
THE break Statement
The break construct is used to break out of the middle of loops: for, do, or while loop. When a break statement is encountered, execution of the current loops immediately stops and resumes at the first statement following the current loop. That is, we can force immediate termination of a loop, bypassing any remaining code in the body of the loop.It is mostly used to exit early from the loop by skipping the remaining statements of loop or switch control structures. It is simply written as
break;
• We can have more than one break statement in a loop.
• The break command terminates only the current loop and not any enclosing loops.
class BreakStatement {
public static void main(String args[]){
System.out.println(“Show importance of break statement”);
for(int i =1; i<=10; i++){
System.out.println(“i = “+i);
if(i==5){
System.out.println(“\nBye”);
break;
}
}
}
}
Output: Show importance of break statement 1 2 3 4 5
i = 1
i = 2
i = 3
i = 4
i = 5
Output:
Bye
Explaination: In this program, the for loop executed starting from i = 1 to 10 in steps of 1. Now when the condition (i==5) in the body of the loop is satisfied, the break statement causes the control to move out of for loop.
Program to input indefinite numbers and then calculate the sum of only the positive numbers. The program terminates when negative number is input?
//program to show sum of indefinite numbers
import java.util.scanner;//program user scanner class
public class SumIndefinite {
public static void main(String[] args){
int num, sum =0;
//Create Scanner object to obtain input from keyboard
Scanner input =newScanner(system.in);
system.out.print(“Enter numbers(negative number to quit) —>”);
while(true){
num = input.nextInt();//Read number
if(num <0)
break;
sum += num;
}
system.out.println(“Sum is —–>”+sum);
}
}
Output: Enter numbers(negative number to quit) —> 50 21 33 17 -1
Sum is —>121
Explanation: This program computes the sum of positive numbers input by the user. When a negative number is an input, the condition (num < 0) become true and break statement executed which leads to the termination of the while loop and the next statement following the loop executed which displays the sum of positive numbers. The condition of the while loop always remains true as we have specified a non-zero value 1 which makes it run infinitely. The only way to exit this loop is to use a break statement.
In the nested loops, if the break statement occurs in the inner loop then the control is transferred only out of the inner loop, and it has nothing to do with the rest of the surrounding looping statements. However, in some cases, we need to jump not only out of the inner loop but also from the outer loop(s). In such a case, Java provides another form of break statement known as a labeled break statement. It allows you to specify from which loop you want to break. The labeled break statement enables you to jump immediately to the statement following the end of any enclosing statement block or loop that is identified by the label in the labeled break statement regardless of how many levels of nested blocks are there.
Before you use a labeled break statement, one should label the statement block or loop you want to exit from. To label a block or loop, you put a label (i.e.label name) followed by a colon at the start of it. Once you have labeled a block or loop, you can use this label along with the break statement. The general form of the labeled break statement is
break label;
On execution, it causes to exit out of the labeled block or loop and resume with the next statement after the labeled break or loop.
Using break as a form of Goto
The break statement can also be used to act as another form of the goto statement. Java does not have a goto statement, as it leads to unstructured programming which is less readable. To come out of a deeply nested set of loops, we can use the labeled break statement. We can also use it to break out of one or more blocks of code. We can also specify precisely the location from where execution should resume because this form of break works with a label as shown :
break label;
the label is the name of a label that identifies a block of code. When this form of break executes, control transferred out of the named block of code. The labeled block of code must enclose the break statement, but it does not need to be the immediately enclosing block. However, we cannot use a break to transfer control to a block of code that does not enclose the break statement.
To name a block, put a label at the start of it. A label is any valid Java identifier followed by a colon. Once we have labeled a block, we can then use this label as the target of a break statement. Doing so causes execution to resume at the end of the labeled block.
THE continue STATEMENT
Like the break statement, the continue statement also skips the remaining statements of the body of the loop where it is defined but instead of terminating the loop, the control is transferred to the beginning of the loop for next iteration. The loop continues until the test condition of the loop becomes false.
When used in the while and do-while loops, the continue statement causes the test condition to be evaluated immediately after it. But in case of for loop, the increment/decrement expression evaluates immediately after the continue statement and then the test condition is evaluated.
It is simply written as
continue;
/* Print Number from 1 to 10 Except 5 */
class NumberExcept {
public static void main(String args[] ) {
int i;
for(i=1;i<=10;i++) {
if(i==5) continue;
System.out.print(i +” “);
}
}
}
Above program will display the value of variable i from 1 to 4. When the value of variable i becomes 5, continue statement will skip the body of the loop following continue statement i.e. it skips System.out.println(i) statement and again executes the loop with the next iteration (value) i.e .. 6.
THE Return Statement
This statement is mainly used in methods in order to terminate a method in between and return back to the caller method. It is an optional statement. That is, even if a method doesn’t include a return statement, control returns back to the caller method after execution of the method. Return statement mayor may not return parameters to the caller method.