Back to: Java Tutorials
The break
and continue
statements are control flow statements in Java that allow a program to alter the flow of execution within a loop. They can be used to make a program more efficient and to create more complex loops.
The break
statement allows a program to exit a loop early. It can be used to stop the loop from continuing to execute when a certain condition is met. When the break
statement is encountered in a loop, the loop is terminated immediately and the program continues to execute the statements that come after the loop. The basic syntax of the break
statement is as follows:
for (initialization; condition; increment) {
// code to be executed
if (condition) {
break;
}
}
Here’s how the break
statement works in a for
loop:
- The
for
loop is initialized with an initial value. - The loop executes as long as the condition is true.
- The
if
statement checks if a certain condition is met. - If the condition is true, the
break
statement is executed and the loop is exited. - The program continues to execute the statements that come after the loop.
The continue
statement, on the other hand, allows a program to skip over the current iteration of a loop and move on to the next one. It can be used to avoid executing certain statements within the loop when a certain condition is met. When the continue
statement is encountered in a loop, the loop skips the current iteration and continues with the next iteration. The basic syntax of the continue
statement is as follows:
for (initialization; condition; increment) {
// code to be executed
if (condition) {
continue;
}
// more code to be executed
}
Here’s how the continue
statement works in a for
loop:
- The
for
loop is initialized with an initial value. - The loop executes as long as the condition is true.
- The
if
statement checks if a certain condition is met. - If the condition is true, the
continue
statement is executed and the current iteration of the loop is skipped. - The loop continues with the next iteration.
- The program executes the statements that come after the
continue
statement within the loop.
Here’s an example program that uses the break
and continue
statements in a loop:
public class BreakContinueStatement {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
if (i == 3 || i == 7) {
continue;
}
System.out.print(i + " ");
}
System.out.println("Loop has ended");
}
}
In this program, the loop executes from 1 to 10. The break
statement is used to exit the loop early when the loop counter is equal to 5. The continue
statement is used to skip the current iteration of the loop when the loop counter is equal to 3 or 7.
The output of the program is as follows:
1 2 4 6 8 9 Loop has ended
As you can see, the loop skipped over the iterations where the loop counter was equal to 3 and 7, and exited the loop when the loop counter was equal to 5.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author