Hello Freinds!!!
Welcome to passionforcs,
In this article, we are going to learn one more flow breaking (jumping) statement i.e. continue. continue statement is another flow breaking statement.
continue statement skips an iteration, when continue occurs control directly transfers to the condition for the next iteration.
In the above example, we can see that when the value of i divides by 2 and gets the reminder 0, continues the loop i.e. skips that iteration otherwise it prints the value of i. when the value of i equals 6 the loop terminates and at last it prints Bye...Bye...
We have one more form of continue statement i.e. continue with label. same as break with label. For example,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//wap to demonstrate continue statement | |
class DemoContinueLabel | |
{ | |
public static void main(String args[]) | |
{ | |
for(int i=1;i<=10;i++){ | |
step: | |
for(int j=1;j<=10;j++) | |
{ | |
if((i*j)%2==0) | |
continue step; | |
System.out.print((i*j)+" "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
In the above example the values of i and j multiplied only for odd numbers when for even numbers it skips the iteration. Try these examples to get more clarity. Happy Coding... :)
No comments:
Post a Comment