Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java Continue Statement

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. In case of inner loop, it continues only inner loop.

Syntax:

continue; statement;
continue;


Java Continue Statement Example

Example:

package com.ashu;
public class Java64bitDemo {
public static void main(String[] ab) {
int j;
for(j=1;j<=5;j++){
if(j==4){
//using continue statement
continue; // It will skip the rest statement
}
System.out.println(j);
}
}
}


Output:1 2 3 5
its 4 not printed on the console. It is because the loop is continued when it reaches to 4

Java Continue Statement with Inner Loop

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

Example:

package com.ashu;
public class Java64bitDemo {
public static void main(String[] ab) {
//outer loop
for(int i=0;i<=3;i++){
//inner loop
for(int j=0;j<=3;j++){
if(i==2&&j==2){
//using continue statement inside inner loop
continue;
}
System.out.println(i+" "+j);
}
}
}
}


Output: 0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 3
3 0
3 1
3 2
3 3

Continue Statement with while loop in java

Example:

package com.ashu;
public class Java64bitDemo {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=8){
if(i==5){
//using continue statement
i++;
continue;//it will skip the rest statement
}
System.out.println(i);
i++;
}
}
}


Output:1
2
3
4
6
7
8
its 5 not printed on the console. It is because the loop is continued when it reaches to 5

Copyright ©2017 Design& develop