logo

Java Continue ステートメント

continue ステートメントは、ループの次の繰り返しにすぐにジャンプする必要がある場合に、ループ制御構造で使用されます。 for ループまたは while ループと一緒に使用できます。

ジャワ 継続ステートメント ループを継続するために使用されます。プログラムの現在のフローを継続し、指定された条件で残りのコードをスキップします。内側のループの場合は、内側のループのみを継続します。

Java の continue ステートメントは、for ループ、while ループ、do-while ループなど、あらゆるタイプのループで使用できます。

構文:

 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 ステートメント

内部ループ内で 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++){>

ラベル付き For ループを含む Java Continue ステートメント

ラベル付きの continue ステートメントを使用できます。この機能は JDK 1.5 以降に導入されました。したがって、Java では、外側のループであっても内側のループであっても、ループを続行できるようになりました。

例:

コアJava面接の質問

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

while ループ内の Java Continue ステートメント

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

do-while ループ内の Java Continue ステートメント

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);>