The continue keyword is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. It is particularly useful for controlling the flow of loops when certain conditions are met, allowing for more readable and efficient code.
Usage
The continue statement can be used within any loop structure to skip the remainder of the code inside the loop for the current iteration and move to the next iteration.
publicclassContinueExample{publicstaticvoidmain(String[] args){for(int i =0; i <10; i++){if(i %2==0){continue;// Skip even numbers}
System.out.println(i);}}}ขับเคลื่อนโดย
In this example, the continue statement is used to skip the even numbers in a for loop. When i is even, the continue statement is executed, and the loop proceeds to the next iteration without printing the value of i.
Example 2: Using continue in a while Loop
publicclassContinueWhileExample{publicstaticvoidmain(String[] args){
int i =0;while(i <10){
i++;if(i %2==0){continue;// Skip even numbers}
System.out.println(i);}}}ขับเคลื่อนโดย
Here, the continue statement is used within a while loop to skip the even numbers. When i is even, the continue statement is executed, and the loop proceeds to the next iteration without printing the value of i.
Example 3: Using continue in a Nested Loop
publicclassContinueNestedLoopExample{publicstaticvoidmain(String[] args){for(int i =1; i <=3; i++){for(int j =1; j <=3; j++){if(j ==2){continue;// Skip when j is 2}
System.out.println("i = "+ i +", j = "+ j);}}}}ขับเคลื่อนโดย
In this nested loop example, the continue statement is used to skip the iteration when j equals 2. This results in the inner loop skipping the print statement whenever j is 2, but continuing with the next iteration.
Additional Example: Using continue with Labelled For Loop
publicclassContinueLabelExample{publicstaticvoidmain(String[] args){outerLoop:for(int i =1; i <=3; i++){for(int j =1; j <=3; j++){if(j ==2){continue outerLoop;// Continue outer loop when j is 2}
System.out.println("i = "+ i +", j = "+ j);}}}}ขับเคลื่อนโดย
In this example, the continue statement is used with a label to skip the current iteration of the outer loop when j equals 2. This control flow allows more complex loop structures to be managed effectively.
Tips and Best Practices
Use Sparingly: Overusing continue can make your code harder to read and understand. Use it sparingly and only when it makes the logic clearer.
Avoid Complex Conditions: Try to avoid using continue with complex conditions as it can make the loop logic difficult to follow.
Nested Loops: Be cautious when using continue in nested loops. It only affects the innermost loop in which it is used.
Alternative Logic: Consider if there is a more straightforward way to achieve the same result without using continue. Sometimes restructuring the loop logic can eliminate the need for continue.
The
continuekeyword is used to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. It is particularly useful for controlling the flow of loops when certain conditions are met, allowing for more readable and efficient code.Usage
The
continuestatement can be used within any loop structure to skip the remainder of the code inside the loop for the current iteration and move to the next iteration.Syntax
Examples
Example 1: Using
continuein aforLoopIn this example, the
continuestatement is used to skip the even numbers in aforloop. Wheniis even, thecontinuestatement is executed, and the loop proceeds to the next iteration without printing the value ofi.Example 2: Using
continuein awhileLoopHere, the
continuestatement is used within awhileloop to skip the even numbers. Wheniis even, thecontinuestatement is executed, and the loop proceeds to the next iteration without printing the value ofi.Example 3: Using
continuein a Nested LoopIn this nested loop example, the
continuestatement is used to skip the iteration whenjequals 2. This results in the inner loop skipping the print statement wheneverjis 2, but continuing with the next iteration.Additional Example: Using
continuewith Labelled For LoopIn this example, the
continuestatement is used with a label to skip the current iteration of the outer loop whenjequals 2. This control flow allows more complex loop structures to be managed effectively.Tips and Best Practices
continuecan make your code harder to read and understand. Use it sparingly and only when it makes the logic clearer.continuewith complex conditions as it can make the loop logic difficult to follow.continuein nested loops. It only affects the innermost loop in which it is used.continue. Sometimes restructuring the loop logic can eliminate the need forcontinue.