Continue Statement
Java continue
Statement
continue
StatementThe continue
statement in Java is a control flow statement that allows you to skip the current iteration of a loop and move on to the next iteration. It is particularly useful when you want to bypass certain parts of a loop based on a condition while still continuing the loop's execution. In this tutorial, we'll explore how the continue
statement works in different contexts and provide detailed examples to help you understand its usage.
1. What is a continue
Statement?
continue
Statement?The continue
statement is used to skip the remaining code in the current iteration of a loop and immediately proceed to the next iteration. It is applicable in for
, while
, and do-while
loops. When the continue
statement is encountered, the control flow jumps to the next iteration of the loop, bypassing any code that follows the continue
statement in the current iteration.
Syntax:
2. Using continue
in Loops
continue
in LoopsThe continue
statement is often used in loops to skip certain iterations based on a condition. This can be helpful when you want to avoid executing specific code for particular cases while still allowing the loop to continue.
2.1 Example: continue
in a for
Loop
Let's start with a simple example where we use the continue
statement to skip printing the number 5 in a for
loop:
Explanation:
The loop starts with
i
equal to 1 and continues untili
is less than or equal to 10.When
i
reaches 5, thecontinue
statement is executed, and the loop skips printingi
for that iteration.
Output:
In this example, the number 5 is skipped, and the loop continues with the next iteration.
2.2 Example: continue
in a while
Loop
You can also use the continue
statement in a while
loop to skip certain iterations:
Explanation:
The loop starts with
i
equal to 1 and continues untili
is less than or equal to 10.When
i
reaches 5, thecontinue
statement is executed, and the loop skips printingi
for that iteration.
Output:
In this example, the number 5 is skipped, and the loop continues with the next iteration.
2.3 Example: continue
in a do-while
Loop
The continue
statement works similarly in a do-while
loop:
Output:
In this example, the number 5 is skipped, and the loop continues with the next iteration.
3. Nested Loops and continue
continue
When you have nested loops, the continue
statement will only affect the innermost loop in which it is used. If you want to skip iterations in an outer loop, you need to use labeled continue
statements.
3.1 Example: continue
in Nested Loops
Explanation:
The outer loop iterates through
i
values from 1 to 3.The inner loop iterates through
j
values from 1 to 3.When
j
equals 2, thecontinue
statement skips the current iteration of the inner loop, but the outer loop continues.
Output:
In this example, the inner loop skips printing when j
equals 2, but the outer loop continues as normal.
4. Labeled continue
Statement
continue
StatementIn situations where you want to skip iterations in an outer loop from within a nested loop, you can use a labeled continue
statement. A label is simply an identifier followed by a colon (:
) placed before a loop.
Example:
Explanation:
The label
outerLoop
is attached to the outer loop.When
j
equals 2, thecontinue outerLoop
statement skips the current iteration of the outer loop, not just the inner loop.
Output:
In this example, both loops skip the iteration when j
equals 2 because the labeled continue
statement affects the outer loop.
Conclusion
The continue
statement is a useful tool in Java that allows you to control the flow of your loops by skipping specific iterations. Whether you're using it to skip certain values in a loop, manage complex nested loops, or control the flow with labeled continue
statements, mastering the continue
statement will make your code more efficient and easier to understand.
For more Java tutorials and resources, visit codeswithpankaj.com.
Last updated