Break and Continue Statements in Java
In certain cases, you may want to alter the standard behavior of a loop. The Java programming language provides branching statements for this purpose that can terminate a loop or skip some of its iterations. Proper use of these statements is essential for writing clear, organized, and efficient code.
In Java, a branching statement is a control flow mechanism that allows for a departure from the standard sequence of statement execution. It enables the program to alter the flow of a loop or conditional statement based on a specific condition. The two main branching statements in Java are break
and continue
. The break
statement terminates the current loop or switch case, while the continue
statement skips the current iteration of a loop. Proper use of branching statements can help introduce complex decision-making capabilities to a Java program.
The break statement
The break statement has two uses:
- it terminates a case in the switch statement (which is a control flow statement used to evaluate an expression against multiple cases);
- it terminates the current loop of any type (for, while, do-while).
In this topic, we will learn how to use it to terminate loops.
In Java, there are different types of loops: for, while, do-while. For loop is often used to iterate over a range of values or through an array. If the number of iterations or the range borders are known, it is recommended to use the for loop. If they are unknown, the while loop may be the preferable solution. A do-while loop is a type of control structure that executes a block of code at least once before checking the condition. It is also known as a post-test loop or an exit-condition loop. The do-while loop consists of three parts: the keyword do, a body, and a condition. The body of the loop is always executed at least once, and then the condition is evaluated. If the condition is true, the loop repeats; if it is false, the loop terminates. A good example of using a do-while loop is a program that reads data from the standard input until a user enters a certain number or string.
The following example demonstrates a loop that includes one break
.
int i = 10;
while (true) { // the condition to continue the loop
if (i == 0) { // the condition to perform the break that stops this loop
break;
}
i--;
}
In the code above, the condition to continue the loop is always true
, but it will be successfully stopped when the variable i
becomes 0
through the use of break
inside the conditional statement.
The break statement terminates only the loop in which it is currently located. If this loop is performed inside another loop, the outer loop won't be stopped.
The following program prints a ladder of numbers.
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + " ");
if (i == j) {
break;
}
}
System.out.println();
}
The break statement can't stop the outer loop (with variable i
) and the code prints:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
To stop the outer loop we could declare a Boolean variable stopped
and use it as a special Boolean flag in the condition of our for loop.
boolean stopped = false;
for (int i = 0; i < 10 && !stopped; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + " ");
if (i == j) {
stopped = true;
break;
}
}
System.out.println();
}
Now, the program's output is not the same:
0
There is another way to stop the outer loop: the labeled break operator. However, it's not considered a good practice to use it because it obscures the control flow making it difficult to follow the logic of the program.
Here's an example demonstrating the use of break
with a label:
outerLoop:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + " ");
if (i == j) {
break outerLoop; // Break out of both loops.
}
}
System.out.println();
}
The continue statement
It causes a loop to skip the current iteration and go to the next one.
This statement can be used inside any kind of loop.
- inside the for-loop, the continue statement causes control to immediately move to the increment/decrement statement;
- inside the while or do-while loop, control immediately moves to the condition.
The following example outputs a sequence of numbers where the odd ones are skipped.
int n = 10;
for (int i = 0; i < n; i++) {
if (i % 2 != 0) {
continue;
}
System.out.print(i + " ");
}
The output:
0 2 4 6 8
The continue statement and the break statement only affect the loop in which they are located. The continue statement cannot skip the current iteration of the outer loop.
Often, we can rewrite our loop without using the continue statement. Here is an example:
int n = 10;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
The result is the same as above, but now we have shorter and more readable code.
It is important to note that the widespread use of branching statements leads to poorly-structured code because conditions in your loops are not actually what you need to do. So, use them wisely — only when it helps to make code shorter and easier to understand for humans.
Conclusion
In Java programming language, the break statement allows for the termination of loops or switch cases based on certain conditions. It can be a valuable tool for controlling the flow of your program, especially when you need to exit loops prematurely. On the other hand, the continue statement is used to skip the current iteration of a loop and proceed to the next one. It is useful for situations where you want to skip specific loop iterations based on certain conditions. However, it's essential to exercise caution when using these statements. Overuse of branching statements can lead to code that is challenging to read and maintain. Therefore, it's recommended to employ them sparingly, primarily when it enhances code clarity and conciseness.