continue Statement in Java28 Jul 2025 | 6 min read The continue statement is used in loop control structure when you need to jump to the next iteration of the loop immediately. It can be used with for loop or while loop. The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition. In case of an inner loop, it continues the inner loop only. Unlike the break statement, continue only interrupts the ongoing iteration and resumes control at the beginning of the next iteration. We can use Java continue statements in all types of loops, such as for loops, while loops, and do-while loops. Syntax: When executed, continue causes the loop to skip the remaining code in the current iteration and immediately proceed to the next iteration. In the case of nested loops, continue affects only the innermost loop. Example: continue Statement//Java Program to demonstrate the use of continue statement inside the for loop. ExampleCompile and RunOutput: 1 2 3 4 6 7 8 9 10 As shown in the output above, the number 5 is not printed to the console because the loop is continued when it reaches 5. Java continue Statement with Inner LoopIt continues the inner loop only if we use the continue statement inside the inner loop. //Java Program to illustrate the use of continue statement inside an inner loop ExampleCompile and RunOutput: 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 Java continue Statement with Labelled for LoopWe can use a continue statement with a label. This feature was introduced in JDK 1.5. Therefore, we can now continue any loop in Java, whether it is an outer loop or an inner loop. ExampleCompile and RunOutput: 1 1 1 2 1 3 2 1 3 1 3 2 3 3 Java continue Statement with While LoopExampleCompile and RunOutput: 1 2 3 4 6 7 8 9 10 Java continue Statement with do-while LoopExampleCompile and RunOutput: 1 2 3 4 6 7 8 9 10 Practical Applications of Continue StatementFiltering DataOne common use case for the continue statement is filtering data. Suppose we have a list of numbers, and we want to print only the odd numbers. Using the continue statement, we can easily skip the even numbers. ExampleCompile and RunOutput: 1 3 5 7 9 Advantages of Continue StatementImproves Readability: It helps you avoid deeply nested if-else structures. The logic becomes more direct and easier to follow. Flexible Control Flow: It enables the efficient handling of unusual instances within loops without altering the primary logic. Example: It skips over null entries cleanly without cluttering the loop body. Useful for Filtering: We can filter unwanted data items during iteration without extra flags or lists. Disadvantages of Continue StatementReduce Clarity if Overused: The loop logic may become difficult to understand if continue is used excessively or with complicated conditions, particularly when there are multiple continue statements. Example: It works, but it is hard to read at a glance. Sometimes, an if with combined conditions might be clearer. ConclusionThe continue statement in Java is a powerful tool for controlling the flow of loops. By skipping specific iterations based on conditions, it helps improve the readability and efficiency of the code. Whether filtering data, avoiding unnecessary computations, or managing complex nested loops, the continue statement proves to be a versatile and valuable tool in a Java programmer's toolkit. Understanding and utilizing continue effectively can lead to cleaner, more maintainable, and efficient code. Java continue Statement MCQs1. What does the continue statement do in a loop?
Answer: B Explanation: The continue statement skips the remaining code in the current iteration and jumps to the next iteration of the loop. 2. In which types of loops can the continue statement be used?
Answer: C Explanation: Java allows continue in all loop types. In for loops, it jumps to the update step; in while and do-while, it re-evaluates the condition. 3. Can the continue statement be used in nested loops?
Answer: C Explanation: continue only affects the loop in which it is directly placed. In nested loops, it skips the current iteration of the innermost loop. 4. What happens when continue is used in a while loop?
Answer: B Explanation: In a while loop, continue causes control to jump to the condition check, skipping the rest of the loop body. 5. What is the output of the following code?
Answer: B Explanation: When i == 2, continue skips the System.out.print() call, so 2 is not printed. Next TopicJava Comments |
We request you to subscribe our newsletter for upcoming updates.