continue keyword in Java

continue keyword in Java

Syntax

// condition
continue;

Program

 
public class continue_demo
{
public static void main(String args[])
{
for(int i=1; i<=10; i++)
{
if(i==6)
{
continue;
}
System.out.println(i);
}
}
}

Output

 
1
2
3
4
5
7
8
9
10

Explanation

The Java continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition.

continue keyword in innerloop

Program

 
public class continue_inner
{
public static void main(String args[])
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
if(i==3 && j==3)
{
continue;
}
System.out.println(i + "-" + j);
}
}
}
}

Output

 
1-1
1-2
1-3
1-4
1-5
2-1
2-2
2-3
2-4
2-5
3-1
3-2
3-4
3-5
4-1
4-2
4-3
4-4
4-5
5-1
5-2
5-3
5-4
5-5

Explanation

It continues inner loop only if you use continue statement inside the inner loop.

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword