Friday, 17 January 2014

JUMP STATEMENTS OR BRANCHING STATEMENTS:continue


Using continue statement


continue:


 statement in java used to skip the part of the loop.Sometimes it is useful to force an early iteration of a loop.Unlike break statement it does not terminate the loop,instead it skip the remaining part of the loop and the control again goes to check the condition again.Continue statement can be used only loop control statements,

such as for loop,while loop and do-while loop.In while and do-while loops,a continue statement causes control to be transferred directly to the conditional expression that controlls the loop.In a for loop,control goes to the iteration portion of the for statement and then to the conditional exprecontinuession.For all three loops,any intermediate code is bypassed.

Let’s look at syntax of break statement:

{
//body of loop
...........
...........
...........
continue;
..........
..........
..........
}

Example for simple break  statement


//Demonstrate continue
class Continue
{
public static void main(String args[])
{
for(int i=0;i<10;i++)
{
System.out.println(i + " ");
if(i%2==0)
continue;
System.out.println(" ");
}
}
}


This code uses the % operator to check if i is even.If it is,the loop continues without printing a new line.Here is the output from this program.


0 1
2 3
4 5
6 7
8 9



No comments :

Post a Comment