Java Break and Continue

Java Break

You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switchstatement.

The break statement can also be used to jump out of a loop.

This example jumps out of the loop when i is equal to 40:

Example

for (int i = 0; i < 100; i++) {
  if (i == 40) {
    break;
  }
  System.out.println(i);
} 

 

Java Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 40:

Example

for (int i = 0; i < 100; i++) {
  if (i == 40) {
    continue;
  }
  System.out.println(i);
}