In this post, you will learn to:
- State the purpose of jump statements.
- Describe break statement.
- Describe continue statement.
- Explain labeled statement.
- Compare break and continue statements.
Purpose
At times, the exact number of times the loop has to be executed is known only during runtime. In such a case, the condition to terminate the loop can be enclosed within the body of the loop. At other times, based on a condition, the remaining statements present in the body of the loop need to be skipped. Java supports jump statements that unconditionally transfer control to locations within a program known as target of jump statements.
Java provides two keywords: break and continue which serve diverse purposes. However, both are used with loops to change the flow of control based on conditions.
‘break’ Statement
The break statement in Java is used in two ways. First, it can be used to terminate a case in the switch statement. Second, it forces immediate termination of a loop, bypassing the loop’s normal conditional test. When the break statement is encountered inside a loop, the loop is immediately terminated and the program control is passed to the statement following the loop.
The following code demonstrates the use of break statement.
Code Snippet:
int cnt, number; for (cnt=1, number=0; cnt <= 100; cnt++) { Scanner input = new Scanner(System.in); System.out.println("Enter a number"); number = input.nextInt(); if(number==500) break; }
As shown in the code, the user is prompted to enter a number, and this is stored in the variable number.This is repeated 100 times. However, if the user enters the number 500, the loop terminates and control is passed to the next statement.
‘continue’ Statement
Java provides another keyword named continue to skip statements within a loop and proceed to the next iteration of the loop.
Code Snippet:
int cnt, square, cube; for (cnt=1 ; cnt <= 10; cnt++) { if(cnt % 3 == 0) continue; square = cnt * cnt; cube = cnt * cnt * cnt; System.out.printf("\nSquare of %d is %d and Cube is %d", cnt, square, cube); }
The code declares a variable cnt and uses the for statement which contains the initialization, termination and increment expression. In the body of the loop, the value of cnt is divided by 3 and the remainder is checked. If the remainder is 0, the continue statement is used to skip the rest of the statements in the body of the loop. If remainder is not 0, the if statement evaluates to false, and the square and cube of cnt is calculated and displayed.
Output:
Square of 1 is 1 and Cube is 1 Square of 2 is 4 and Cube is 8 Square of 4 is 16 and Cube is 64 Square of 5 is 25 and Cube is 125 Square of 7 is 49 and Cube is 343 Square of 8 is 64 and Cube is 512 Square of 10 is 100 and Cube is 1000
Labeled Statement
A labeled statement is used only in case of nested loops. It is used to indicate the nested loop that is to be continued with the next iteration, or the nested loop to break from. A break keyword, when used with a label, exits out of the labeled loop.
The following code demonstrates the use of labeled break statement.
Code Snippet:
..... int i; outer: for(i=0; i<5; i++) { if(i == 2) { System.out.println("Hello"); break outer; } System.out.println("This is the outer loop."); } System.out.println("Good-Bye"); .....
As shown in the code, the loop is supposed to be executed five times. The first two times it displays the sentence ‘This is the outer loop’. In the third pass the value of i is equal to 2, thus it enters the if statement and prints ‘Hello’. Next, the break statement is encountered and the control passes to the label outer:. Thus, the loop is terminated and the last statement is printed.
The output of the code will be:
This is the outer loop. This is the outer loop. Hello Good-Bye
The following code demonstrates the use of labeled continue statement.
Code Snippet:
..... outer: for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { System.out.println("Hello"); continue outer; } System.out.println("This is the outer loop."); } System.out.println("Good-Bye"); .....
In the code, ‘Hello’ will be printed five times. After the continue statement is executed, the flow continues with the next iteration of the loop identified with the label. Finally, when the condition in the outer loop evaluates to false, this loop will exit and ‘Good-Bye’ will be printed.
Thus, the output of the code will be as follows:
Hello Hello Hello Hello Hello Good-Bye
Differecen between break and continue
The table below lists the features of jump statements.
break | continue |
---|---|
break statement is used with a condition within a loop. If the condition is true, then the flow of control passes to the statement outside the loop. The remaining iterations are not executed. | continue statement is used with a condition within a loop. If the condition is true, then all statements succeeding continue, if there are any, are ignored and the next iteration is carried out. |
break can be used with all loops. | continue can be used with all loops. |