0% found this document useful (0 votes)
21 views

Java Continue Statement

Uploaded by

Vikas Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Java Continue Statement

Uploaded by

Vikas Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Continue Statement

The continue statement is used in loop control structure when you need to jump to the next iteration of
the loop immediately. It can be used with for loop or while loop.

The Java continue statement is used to continue the loop. It continues the current flow of the program
and skips the remaining code at the specified condition. In case of an inner loop, it continues the inner
loop only.

Java Continue Statement Example


Example:

//Java Program to demonstrate the use of continue statement


//inside the for loop.
public class ContinueExample {
public static void main(String[] args) {
//for loop
for(int i=1;i<=10;i++){
if(i==5){
//using continue statement
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
}

Output:

1
2
3
4
6
7
8
9
10

Java Continue Statement with Inner Loop


Example:

//Java Program to illustrate the use of continue statement


//inside an inner loop

ASR 1
public class ContinueExample2 {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using continue statement inside inner loop
continue;
}
System.out.println(i+" "+j);
}
}
}
}

Output:

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

Java Continue Statement with Labeled For Loop


We can use continute statement with a label. This feature is introduced since JDK 1.5. So, we can continue
any loop in Java now whether it is outer loop or inner.

Example:

//Java Program to illustrate the use of continue statement


//with label inside an inner loop to continue outer loop
public class ContinueExample3 {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using continue statement with label
continue aa;
}

ASR 2
System.out.println(i+" "+j);
}
}
}
}

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java Continue Statement in while loop


Example:

//Java Program to demonstrate the use of continue statement


//inside the while loop.
public class ContinueWhileExample {
public static void main(String[] args) {
//while loop
int i=1;
while(i<=10){
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
9

ASR 3
10

Java Continue Statement in do-while loop


Example:

//Java Program to demonstrate the use of continue statement


//inside the Java do-while loop.
public class ContinueDoWhileExample {
public static void main(String[] args) {
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
//using continue statement
i++;
continue;//it will skip the rest statement
}
System.out.println(i);
i++;
}while(i<=10);
}
}

Output:

1
2
3
4
6
7
8
9
10

ASR 4

You might also like