Header Ads Widget

Responsive Advertisement

break and continue statements

This tutorial provides an overview of how to utilize the break and continue statements in Java. When working with loops in Java, these statements can be particularly useful. The continue statement allows you to skip the current iteration of a loop and proceed to the next one. This concept is demonstrated in the following code snippet:

continue statement

System.out.println("continue when i is 2:");
            for (int i = 1; i <= 3; i++) {
  if (i == 2) {
   System.out.print("[continue]");
   continue;
  }
  System.out.print("[i:" + i + "]");
 }

 

The result produced by the aforementioned code is displayed here. It is important to observe that the code following the continue statement does not execute after the second iteration, yet the third iteration is still carried out. continue when i is 2: [i:1][continue][i:3]

 

break statement

The break statement terminates a loop. It exits the current iteration and concludes the immediate loop.

System.out.println("\nbreak when i is 2:");
 for (int i = 1; i <= 3; i++) {
  if (i == 2) {
   System.out.print("[break]");
   break;
  }
  System.out.print("[i:" + i + "]");
 }

 

The code provided produces the subsequent output. During the second iteration, we exit the loop, which means that any code following the break statement in that iteration is not executed, and the third iteration does not take place. The break occurs when i equals 2: [i:1][break].

 

 

break and continue to a label

 

The break and continue statements in Java can be directed to specific labels, which is the closest functionality to a goto statement in the language. Utilizing labeled breaks and continues is particularly beneficial when dealing with nested loops. The code example provided demonstrates nested loops where a break is applied to the label of the outer loop.

 

break to a label

System.out.println("\nbreak to outer when i is 2 and j is 2:");
 kartik: for (int i = 1; i <= 3; i++) { // outer loop
  for (int j = 1; j <= 3; j++) { // inner loop
   if ((i == 2) && (j == 2)) {
    System.out.print("[break to outer]");
    break kartik;
   }
   System.out.print("[i:" + i + ",j:" + j + "]");
  }
 }

 

The results are displayed here. It is important to observe that breaking out of the outer loop will also terminate both loops. The break to the outer loop occurs when i equals 2 and j equals 2: [i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][break to outer].

 

Another one example

Below, we can see an example of breaking to an inner loop.

 System.out.println("\nbreak to inner when i is 2 and j is 2:");
 for (int i = 1; i <= 3; i++) { // outer loop
  kartik: for (int j = 1; j <= 3; j++) { // inner loop
   if ((i == 2) && (j == 2)) {
    System.out.print("[break to inner]");
    break kartik;
   }
   System.out.print("[i:" + i + ",j:" + j + "]");
  }
 }

 

The code presented generates the output shown below. It is important to observe that breaking to the inner label ends the current inner loop, after which the next iteration of the outer loop continues. Break to inner when i is 2 and j is 2: [i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][break to inner][i:3,j:1][i:3,j:2][i:3,j:3].

 

continue to a label

Next, we can see an example of continuing to an outer loop label.

System.out.println("\ncontinue to outer when i is 2 and j is 2:");
 kartik: for (int i = 1; i <= 3; i++) {  // outer loop
  for (int j = 1; j <= 3; j++) { // inner loop
   if ((i == 2) && (j == 2)) {
    System.out.print("[continue to outer]");
    continue kartik;
   }
   System.out.print("[i:" + i + ",j:" + j + "]");
  }
 }

 

The result produced by the aforementioned code is displayed here. It is evident that invoking the continue statement for the outer loop label terminates the current inner loop, allowing us to move on to the subsequent iteration of the outer loop. When the values of i and j are both 2, the sequence is as follows: [i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][continue to outer][i:3,j:1][i:3,j:2][i:3,j:3].

 

Another one example:

Here, we can see continuing to an inner loop label.

System.out.println("\ncontinue to inner when i is 2 and j is 2:");
 for (int i = 1; i <= 3; i++) { // outer loop
  kartik: for (int j = 1; j <= 3; j++) { // inner loop
   if ((i == 2) && (j == 2)) {
    System.out.print("[continue to inner]");
    continue kartik;
   }
   System.out.print("[i:" + i + ",j:" + j + "]");
  }
 }

 

The output provided illustrates that when a continue statement is encountered within an inner loop, it bypasses the remaining code for the current iteration of that inner loop, proceeding directly to the next iteration. For instance, when i equals 2 and j equals 2, the sequence is as follows: [i:1,j:1][i:1,j:2][i:1,j:3][i:2,j:1][continue to inner][i:2,j:3][i:3,j:1][i:3,j:2][i:3,j:3].

break and continue statements
break and continue statements


Post a Comment

0 Comments