TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java Continue

Java continue statement with concepts and examples, java continue statement with labeled for loop and difference between break and continue in java.

<< Back to JAVA

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.

We can use Java continue statement in all types of loops such as for loop, while loop and do-while loop.

Syntax:

jump-statement;  
continue; 

Java Continue Statement Example

ContinueExample.java

//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);
    }
}
}
Test it Now

Output:

1
2
3
4
6
7
8
9
10

As you can see in the above output, 5 is not printed on the console. It is because the loop is continued when it reaches to 5.

Java Continue Statement with Inner Loop

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

ContinueExample2.java

//Java Program to illustrate the use of continue statement
//inside an inner loop
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 Labelled For Loop

We can use continue 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:

ContinueExample3.java

//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;  
	                    }  
	                    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

ContinueWhileExample.java

//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++;
    }
}
}
Test it Now

Output:

1
2
3
4
6
7
8
9
10

Java Continue Statement in do-while Loop

ContinueDoWhileExample.java

//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);
}
}
Test it Now

Output:

1
2
3
4
6
7
8
9
10

Next TopicJava Comments




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf