Unlike 'break' jump statement which completely terminates the loop, 'continue' jump statement only skips the current iternation and continues with the next iteration.
Program to demonstrate the 'continue' jump statement ( 'for' loop) |
class ContinueDemo { public static void main(String args[]) { for(int i=0;i<=10;i++) { if(i%2==0) continue; System.out.print(i+" "); } } } Output of this program: 1 3 5 7 9 Observe that the if 'continue' statement is not provided, the program would have printed 0 1 2 3 4 5 6 7 8 9 10 values, but why did it print only 1 3 5 7 9. Because when the program comes across 'continue' statement in the loop, it will just stop current iteration and go to the next iteration. i.e. The statement before the continue statement will be executed, but statements after the 'continue' statement wont be executed as the continue statement takes the control to the next iteration. Since the above program evaluates the condition if(i%2==0) i.e. If the value of i when divided by 2 results in 0, then the condition gets passed and the continue statement gets executed. Hence the values 0,2,4,6,8 and 10 are not printed by the above program. |
Program to demonstrate the 'continue' jump statement ( 'while' loop) |
class ContinueWhileLoop { public static void main(String args[]) { int i=0; while(i<=10) { if(i%2==0) { i++; // We've to mention it here, otherwise the while loop iteration will stop continue; } System.out.print(i+" "); i++; } } }
Output of this program:
1 3 5 7 9
Unlike 'for' loop, which has incremental operation in the 'for' statement itself, the 'while' loop variable incremental should be specified in the loop itself. But when the continue statement gets executed the program execution wont execute the print and i++ statements. If it happens, this program wont print anything. Hence we've to increment the variable when the if(i%2==0) is true. i.e. before the program executing the 'continue' statement as shown in this example.
|
Program to demonstrate the 'continue' jump statement ( 'do-while' loop) |
class ContinueDoWhileLoop { public static void main(String args[]) { int i=0; do{ if(i%2==0) { i++; continue; } System.out.print(i+" "); i++; }while(i<=10); } }
Output of this program:
1 3 5 7 9
Unlike 'for' loop, which has incremental operation in the 'for' statement itself, the 'do-while' loop variable incremental should be specified in the loop itself. But when the continue statement gets executed the program execution wont execute the print and i++ statements. If it happens, this program wont print anything. Hence we've to increment the variable when the if(i%2==0) is true. i.e. before the program executing the 'continue' statement as shown in this example.
|
Using continue to transfer the control to the outer loop |
class ContinueOuterLoop { public static void main(String args[]) { outer: for(int i=0;i<=5;i++) //This loop is labelled as 'outer' { System.out.println(); inner: for(int j=0;j<=5;j++) //This loop is labelled as 'inner' { if(j>i) continue outer; //If this statement gets executed, the program execution control will be transferred to the loop labelled as 'outer' System.out.print(j+" "); } } } }
Output of this program:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Observe that when ever the condition in the inner loop results true, the continue outer; loop statement gets executed and the program execution control will be transferred to the outer loop.
|
Please comment below to feedback or ask questions.
The 'class' concept will be explained in the next post.
No comments:
Post a Comment