logo

Java Continue-Anweisung

Die continue-Anweisung wird in der Schleifenkontrollstruktur verwendet, wenn Sie sofort zur nächsten Iteration der Schleife springen müssen. Es kann mit einer for-Schleife oder einer while-Schleife verwendet werden.

Das Java continue-Anweisung wird verwendet, um die Schleife fortzusetzen. Es setzt den aktuellen Programmablauf fort und überspringt den verbleibenden Code unter der angegebenen Bedingung. Im Falle einer inneren Schleife wird nur die innere Schleife fortgesetzt.

Wir können die Java-Continue-Anweisung in allen Arten von Schleifen verwenden, z. B. in der for-Schleife, der while-Schleife und der do-while-Schleife.

Syntax:

 jump-statement; continue; 

Beispiel für eine Java Continue-Anweisung

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 } system.out.println(i); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <p>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.</p> <h2>Java Continue Statement with Inner Loop</h2> <p>It continues inner loop only if you use the continue statement inside the inner loop.</p> <p> <strong>ContinueExample2.java</strong> </p> <pre> //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&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using continue statement inside continue; } system.out.println(i+' '+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 </pre> <h2>Java Continue Statement with Labelled For Loop</h2> <p>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.</p> <p> <strong>Example:</strong> </p> <p> <strong>ContinueExample3.java</strong> </p> <pre> //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&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using continue statement with label aa; } system.out.println(i+' '+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 3 1 3 2 3 3 </pre> <h2>Java Continue Statement in while loop</h2> <p> <strong>ContinueWhileExample.java</strong> </p> <pre> //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 } system.out.println(i); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <h2>Java Continue Statement in do-while Loop</h2> <p> <strong>ContinueDoWhileExample.java</strong> </p> <pre> //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); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <hr></=10);></pre></=10){></pre></=3;i++){></pre></=3;i++){></pre></=10;i++){>

Wie Sie in der obigen Ausgabe sehen können, wird 5 nicht auf der Konsole gedruckt. Dies liegt daran, dass die Schleife fortgesetzt wird, wenn sie 5 erreicht.

Java Continue-Anweisung mit innerer Schleife

Die innere Schleife wird nur dann fortgesetzt, wenn Sie die continue-Anweisung innerhalb der inneren Schleife verwenden.

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&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using continue statement inside continue; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 </pre> <h2>Java Continue Statement with Labelled For Loop</h2> <p>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.</p> <p> <strong>Example:</strong> </p> <p> <strong>ContinueExample3.java</strong> </p> <pre> //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&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using continue statement with label aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 3 1 3 2 3 3 </pre> <h2>Java Continue Statement in while loop</h2> <p> <strong>ContinueWhileExample.java</strong> </p> <pre> //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 } system.out.println(i); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <h2>Java Continue Statement in do-while Loop</h2> <p> <strong>ContinueDoWhileExample.java</strong> </p> <pre> //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); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <hr></=10);></pre></=10){></pre></=3;i++){></pre></=3;i++){>

Java Continue-Anweisung mit beschrifteter For-Schleife

Wir können die continue-Anweisung mit einer Bezeichnung verwenden. Diese Funktion wird seit JDK 1.5 eingeführt. Wir können also jetzt jede Schleife in Java fortsetzen, unabhängig davon, ob es sich um eine äußere oder eine innere Schleife handelt.

Beispiel:

Java-String-Charat

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&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using continue statement with label aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 3 1 3 2 3 3 </pre> <h2>Java Continue Statement in while loop</h2> <p> <strong>ContinueWhileExample.java</strong> </p> <pre> //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 } system.out.println(i); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <h2>Java Continue Statement in do-while Loop</h2> <p> <strong>ContinueDoWhileExample.java</strong> </p> <pre> //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); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <hr></=10);></pre></=10){></pre></=3;i++){>

Java Continue-Anweisung in der While-Schleife

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 } system.out.println(i); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <h2>Java Continue Statement in do-while Loop</h2> <p> <strong>ContinueDoWhileExample.java</strong> </p> <pre> //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); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <hr></=10);></pre></=10){>

Java Continue-Anweisung in der do-while-Schleife

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); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 6 7 8 9 10 </pre> <hr></=10);>