Continue Statement

Continue statement is used to skip part of loop. break statement terminate the loop but continue statement make skipping of loop statements appearing after continue statement. Continue statement must be used inside the loop. In case of multiple loop 'continue' statement will skip end of the innermost loop, and transfer control to conditional statement controlling loop.


Syntax of unlabelled continue

   { 
loop body
...........
...........
...........
continue;
...........
...........
...........
}

All statement after continue statement will be skipped on execution of continue statement. This continue statement is unlabelled version. like break, continue also has labelled version.

Example :


// file : cont.java
// www.jgyan.com

class cont
{
public static void main(String ...arg)
{
int i;
for(i=0;i<100;i++)
{
if ( i%2==0)
continue;
System.out.println("Number:"+i);
}

}
}

Syntax of labelled continue

Syntax :

   { 
loop body
...........
...........
...........
continue label;
...........
...........
...........
}

labelled version skip current iteration of outer loop following label, specified with continue statement.

Example :


// file : cont1.java
// www.jgyan.com
class cont1
{
public static void main(String ...arg)
{
int i;
logo:
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if ( i==j)
{
continue logo;
}
System.out.println("Number:"+j);
}
System.out.println("Number:"+j);
}
}
}

Above program demonstrating use of labelled continue statement. When 'continue logo;' statement occurred in program. It will skip current iteration of outer loop ( for(i=0;i<100;i++) {} ), and again then conditional statement(i<10) is evaluated.