Java - continue with Label

Introduction

An unlabeled continue statement continues the current for loop, while loop, and do-while loop.

A labeled continue statement can continue in the outer loop.

Example

The following code shows how to print lower half of the 3x3 matrix using a continue statement.

Demo

public class Main {
  public static void main(String[] args) {
    outer: // The label "outer" starts here
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        System.out.print(i + "" + j);
        System.out.print("\t");
        if (i == j) {
          System.out.println(); // Print a new line
          continue outer; // Continue the outer loop
        }/*from   ww  w. j a va2 s .c o m*/

      }
    }
  }
}

Result

Related Topic