Labeled Continue Statement in if Statement Java
In this tutorial, we will learn about the continue
statement and labeled continue
statement in Java with the help of examples.
While working with loops, sometimes we need to skip statements inside the loop or terminate the loop immediately without checking the test condition. In those situations, break
and continue
statements are used.
Here we will learn about the continue
statement, to learn more about break
statement, you can visit Java break.
Java continue Statement
The continue
statement is used to skip the current iteration of a loop (for, while, etc).
When the continue
statement is executed, the program directly jumps to the end of the loop. And, the test statement is evaluated (update statement for the for loop); after this, the loop continues with the next iteration.
The syntax of the continue
statement can be given as follows:
continue ;
How continue Statement works?
The following illustration shows how the continue
statement works with different loops.
Example: Java continue Statement with a for loop
In the following example, we will use the continue
statement with a for loop.
public class Main { public static void main ( String [ ] args) { for ( int i = 0 ; i <= 8 ; i++ ) { // if value of i is between 4 and 7, continue is executed if (i > 3 && i < 7 ) { continue ; } System .out. println (i) ; } } }
Output:
0 1 2 3 7 8
As we can see above, we used the for
loop to print the value of i
in each iteration.
if (i > 3 && i < 7 ) { continue ; }
Here, the above continue
statement is executed when the value of i
becomes more than 3 and less than 7. So it skips to print statements for values 4, 5, 6.
Example: Java continue Statement with a while loop
In the following example, we will calculate the sum of 6 positive numbers entered by the user.
import java.util. Scanner ; public class Main { public static void main ( String [ ] args) { int number, sum, i = 0 ; // creating an object of Scanner class Scanner input = new Scanner ( System .in) ; while (i < 6 ) { // taking integer input from the user System .out. println ( "Enter a positive number : " ) ; number = input. nextInt ( ) ; i++ ; if (number <= 0 ) { continue ; } sum += number; } System .out. println ( "Sum = " + sum) ; input. close ( ) ; } }
Output:
Enter a positive number : 6 Enter a positive number : 3 Enter a positive number : 2 Enter a positive number : - 7 Enter a positive number : - 2 Enter a positive number : 9 Sum = 20
As we can see above, we have used the while loop to calculate the sum of 6 positive numbers.
if (number <= 0 ) { continue ; }
Here, the continue
statement is executed when the user enters a negative number. This skips the current iteration of the loop and jumps the program control to the next iteration of the loop.
Java continue with Nested Loop
Using the continue
statement with nested loops skips the current iteration of the innermost loop.
As we can see above, the continue
skips the current iteration of the innermost while loop, and the control jumps to the next iteration.
Example: continue Statement with Nested Loop
In the following example, we will use the continue
statement with a nested loop.
public class Main { public static void main ( String [ ] args) { int i = 0 , j = 0 ; // outer loop while (i <= 2 ) { // inner loop while (j <= 2 ) { if (j == 1 ) { j++ ; continue ; } System .out. println ( "Inner Loop: " + j) ; j++ ; } i++ ; } } }
Output
Outer Loop : 0 Inner Loop : 0 Inner Loop : 2 Outer Loop : 1 Outer Loop : 2
As we can see above, we have used the continue
statement inside the inner loop.
if (j == 1 ) { j++ ; continue ; }
Here, when the value of j
is 1, j
is increased, and the continue
statement is executed. This skips the iteration of the inner loop. So, the text Inner Loop: 1
is skipped.
Labeled continue Statement
We have already used the unlabeled continue
statement, which skips the current iteration of the innermost loop statement. However, there is another form of break statement known as the labeled continue.
The syntax of the continue
statement can be given as follows:
continue label;
The labeled continue
statement skips the current loop iteration specified by label
.
In the illustration above, the continue
statement skips the current iteration of the labeled statement. Then, the program control jumps to the next iteration of the labeled statement.
Example: Labeled continue Statement
In the following example, we will use the labeled continue
statement with for loop.
public class Main { public static void main ( String [ ] args) { first: for ( int i = 0 ; i < 4 ; i++ ) { for ( int j = 0 ; j < 2 ; j++ ) { if (i == 1 ) { continue first; } System .out. println ( "i = " + i + "; j = " + j) ; } } } }
Output
i = 0 ; j = 0 i = 0 ; j = 1 i = 2 ; j = 0 i = 2 ; j = 1 i = 3 ; j = 0 i = 3 ; j = 1
As we can see above, the labeled continue
statement is used to skip the current iteration when the i == 1
is true of the loop labeled as first
.
Note: The use of
labeled continue
is not recommended because it makes the code hard to read and understand. If you are in a situation where you need to use thelabeled continue
, it's time to refactor the code and use a different approach.
Source: https://www.expectocode.com/tuto/java/java_continue_statement/
0 Response to "Labeled Continue Statement in if Statement Java"
Post a Comment