Java Continue and Break Statement
In Java, loops are often used to execute a block of code repeatedly. However, sometimes we need to skip certain iterations or stop the loop entirely based on a specific condition. This is where the continue
and break
statements come into play. They help control the flow of the loop.
In this blog, we will explore how to use the continue
and break
statements, understand their purpose, and see how they work in different types of loops.
What is the continue
Statement?#
The continue
statement in Java is used to skip the current iteration of a loop and continue with the next iteration. When the continue
statement is encountered inside a loop, it forces the loop to move directly to the next iteration without executing the remaining code in the current iteration.
Syntax of continue
Statement#
The continue
statement can only be used inside loops (for
, while
, do-while
). When encountered, it skips the remaining code inside the loop for the current iteration and jumps to the next iteration of the loop.
Example 1: Using continue
in a for
Loop#
Let's see how the continue
statement works in a for loop.
Explanation:
- The
if (i % 2 == 0)
condition checks if the number is even. - If the number is even, the
continue
statement is triggered, and theSystem.out.println(i);
is skipped for that iteration. - As a result, only odd numbers (1, 3, 5, 7, 9) are printed.
Expected Output:
What is the break
Statement?#
The break
statement in Java is used to terminate the execution of the loop immediately, regardless of the loop's condition. When the break
statement is executed inside a loop, it exits the loop and continues with the next statement after the loop.
Syntax of break
Statement#
The break
statement can be used in any loop (for
, while
, do-while
) and in switch statements to exit early.
Example 2: Using break
in a for
Loop#
Let's see how the break
statement works in a for loop.
Explanation:
- The loop starts from 1 and prints numbers.
- When
i
equals 6, thebreak
statement is triggered, and the loop is immediately exited. - The loop terminates before printing numbers 6 to 10.
Expected Output:
Using continue
and break
in a while
Loop#
Both the continue
and break
statements can also be used in while loops. Let’s see some examples.
Example 3: Using continue
in a while
Loop#
Explanation:
- The
continue
statement skips the printing of even numbers. - The loop prints only odd numbers from 1 to 9.
Expected Output:
Example 4: Using break
in a while
Loop#
Explanation:
- The loop prints numbers from 1 to 5.
- The
break
statement is triggered wheni
equals 6, and the loop is terminated.
Expected Output:
continue
and break
with Nested Loops#
In Java, you can also use continue
and break
in nested loops (loops inside loops). By default, these statements affect only the innermost loop. However, you can use labeled continue
and break
to control outer loops as well.
Example 5: Using continue
in Nested Loops#
Explanation:
- The outer loop iterates over
i
, and the inner loop iterates overj
. - The
continue
statement skips the iteration when bothi
equals 2 andj
equals 2. - The loop will print all pairs of
i
andj
, except wheni == 2
andj == 2
.
Expected Output:
Example 6: Using break
in Nested Loops#
Explanation:
- The
break
statement exits the inner loop wheni == 2
andj == 2
. - The inner loop terminates early, and the outer loop continues its next iteration.
Expected Output:
Conclusion#
In this blog, we have learned:
- The
continue
statement skips the current iteration of a loop and moves to the next one. - The
break
statement immediately exits the loop and continues with the next statement after the loop. - We explored how to use
continue
andbreak
in different types of loops (for, while) and with nested loops. - These control statements are powerful tools for managing the flow of loops and making your code more efficient and readable.