loading

Java Break/Continue

Java Break

The break statement was already utilized in a previous chapter of this lesson. A switch statement was “jumped out” of using it.

Another way to exit a loop is with the break statement.

In this example, the loop is terminated when i reaches 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.

This example skips the value of 4:

Example

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

Break and Continue in While Loop

In while loops, you may also use break and continue:

Break Example

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

Continue Example

				
					int i = 0;
while (i < 10) {
  if (i == 4) {
    i++;
    continue;
  }
  System.out.println(i);
  i++;
}
				
			
Share this Doc

Java Break/Continue

Or copy link

Explore Topic