Previous Next

Java Break and Continue

Break Statement

The break statement is employed to exit the loop where it is found. The break statement is employed within loops or switch statements in C programming.

Example:

public class Main {
  public static void main(String[] args) {
    int i;
    for (i = 0; i < 10; i++) {
      if (i == 6) {
        break;
      }
      System.out.println(i);
    }
  }
}

Output:

1
2
3
4
5

Continue Statement

The continue statement bypasses the current iteration of the loop and moves on to the next.

Example:

public class Main {
  public static void main(String[] args) {
    int i;
    for (i = 1; i < 10; i++) {
      if (i == 3) {
       continue;
      }
      System.out.println(i);
    }
  }
}

Output:

1
2
4
5
6
7
8
9
Previous Next