google Ads

Thursday, July 9, 2009

Continue Statement

The continue Statement


The continue statement is used to continue within loops to jump to another statement

The continue statement can only be called form within a loop.

The continue statement has two forms: unlabeled and labeled. If you use the unlabelled form, control transfers to the termination condition of the loop. The termination condition gets re-evaluated at this point and the loop either continues or not depending on the results of the test. In for and while loops, this means that control returns to the top of the loop. In do-while loops, this means that controls returns to the bottom of the loop.

The labeled form of the continue statement continues at the next iteration of the labeled loop. Consider this implementation of the String class’s indexOf method which uses the labeled form of continue:

Example for continue statement.


public class continueDemo
{
public static void main(String args[])
{
int n=6;
System.out.println(“Initial Value of n is: “+n);
step1:
while(n>1)
{
System.out.println(“Now the N value is : “+n);
--n;
if(n<=1)
continue step1;

}
System.out.println(“The out of the while loop! “);
}
}


The Output will be:

Initial Value of n is: 6

Now the N value is : 6
Now the N value is : 5
Now the N value is : 4
Now the N value is : 3
Now the N value is : 2

The out of the while loop!

No comments:

Post a Comment