Java Break/continue

Java Break

The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

The break keyword is used to break out a for loop, a while loop or a switch block.

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

The break statement in Java terminates the loop immediately, and the control of the program moves to the next statement following the loop. It is almost always used with decision-making statements .

This example stops the loop when i is equal to 4:

Example

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    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.

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

This example skips the value of 4:

Example

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

Leave a comment

Design a site like this with WordPress.com
Get started