logo

הצהרת Java Continue

ההצהרה continue משמשת במבנה בקרת הלולאה כאשר אתה צריך לקפוץ לאיטרציה הבאה של הלולאה באופן מיידי. ניתן להשתמש בו עם לולאה עבור או בעוד לולאה.

הג'אווה להמשיך בהצהרה משמש להמשך הלולאה. זה ממשיך את הזרימה הנוכחית של התוכנית ומדלג על הקוד שנותר במצב שצוין. במקרה של לולאה פנימית, הוא ממשיך את הלולאה הפנימית בלבד.

אנו יכולים להשתמש בהצהרת Java continue בכל סוגי הלולאות כגון for loop, while loop ו-do-while loop.

תחביר:

 jump-statement; continue; 

דוגמה להצהרת Java Continue

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++){>

כפי שניתן לראות בפלט לעיל, 5 אינו מודפס על הקונסולה. הסיבה לכך היא שהלולאה נמשכת כשהיא מגיעה ל-5.

הצהרת Java Continue with Inner Loop

זה ממשיך לולאה פנימית רק אם אתה משתמש במשפט continue בתוך הלולאה הפנימית.

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 עם התווית For Loop

אנו יכולים להשתמש בהצהרת המשך עם תווית. תכונה זו מוצגת מאז JDK 1.5. אז, אנחנו יכולים להמשיך כל לולאה ב-Java כעת, בין אם היא לולאה חיצונית או פנימית.

דוגמא:

c# מכיל מחרוזת

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