logo

Hurok Java nyelven

A Java hurokhoz a program egy részének többszöri ismétlésére szolgál. Ha az iterációk száma rögzített , a loop használata javasolt.

A Java-ban háromféle for ciklus létezik.

Hurok Java nyelven
  • Egyszerű a Loop számára
  • Az egyes vagy Enhanced for Loop
  • Loop címkével

Java Simple for Loop

Az egyszerű for ciklus ugyanaz, mint C / C++ . Inicializálhatjuk a változó , ellenőrizze a feltételt és növelje/csökkentse az értéket. Négy részből áll:

    Inicializálás: Ez a kezdeti feltétel, amely egyszer végrehajtásra kerül, amikor a ciklus elindul. Itt inicializálhatjuk a változót, vagy használhatunk egy már inicializált változót. Ez egy opcionális feltétel.Feltétel: Ez a második feltétel, amely minden alkalommal végrehajtódik a hurok állapotának tesztelésére. A végrehajtást addig folytatja, amíg a feltétel hamis lesz. Igaz vagy hamis logikai értéket kell visszaadnia. Ez egy opcionális feltétel.Növekedés/csökkentés: Növeli vagy csökkenti a változó értékét. Ez egy opcionális feltétel.Nyilatkozat: A ciklus utasítása minden alkalommal végrehajtódik, amíg a második feltétel hamis nem lesz.

Szintaxis:

 for(initialization; condition; increment/decrement){ //statement or code to be executed } 

Folyamatábra:

for ciklus a java folyamatábrán

Példa:

bináris kereső python

Például.java

 //Java Program to demonstrate the example of for loop //which prints table of 1 public class ForExample { public static void main(String[] args) { //Code of Java for loop for(int i=1;i<=10;i++){ system.out.println(i); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>Java Nested for Loop</h2> <p>If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.</p> <p> <strong>Example:</strong> </p> <p> <strong>NestedForExample.java</strong> </p> <pre> public class NestedForExample { public static void main(String[] args) { //loop of i for(int i=1;i<=3;i++){ loop of j for(int system.out.println(i+' '+j); } end i < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <p> <strong>Pyramid Example 1:</strong> </p> <p> <strong>PyramidExample.java</strong> </p> <pre> public class PyramidExample { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j="1;j&lt;=i;j++){" system.out.print('* '); } system.out.println(); new line < pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * </pre> <p> <strong>Pyramid Example 2:</strong> </p> <p> <strong>PyramidExample2.java</strong> </p> <pre> public class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i=i;j--){ System.out.print(&apos;* &apos;); } System.out.println();//new line } } } </pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * * * * * * * </pre> <h2>Java for-each Loop</h2> <p>The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don&apos;t need to increment value and use subscript notation.</p> <p>It works on the basis of elements and not the index. It returns element one by one in the defined variable.</p> <p> <strong>Syntax:</strong> </p> <pre> for(data_type variable : array_name){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForEachExample.java</strong> </p> <pre> //Java For-each loop example which prints the //elements of the array public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 12 23 44 56 78 </pre> <h2>Java Labeled For Loop</h2> <p>We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop.</p> <h4>Note: The break and continue keywords breaks or continues the innermost for loop respectively.</h4> <p> <strong>Syntax:</strong> </p> <pre> labelname: for(initialization; condition; increment/decrement){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>LabeledForExample.java</strong> </p> <pre> //A Java program to demonstrate the use of labeled for loop public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" break aa; } system.out.println(i+' '+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <p>If you use <strong>break bb;</strong> , it will break inner loop only which is the default behaviour of any loop.</p> <p> <strong>LabeledForExample2.java</strong> </p> <pre> public class LabeledForExample2 { 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){" break bb; } 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 Infinitive for Loop</h2> <p>If you use two semicolons ;; in the for loop, it will be infinitive for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(;;){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForExample.java</strong> </p> <pre> //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c </pre> <p>Now, you need to press ctrl+c to exit from the program.</p> <h2>Java for Loop vs while Loop vs do-while Loop</h2> <table class="table"> <tr> <th>Comparison</th> <th>for loop</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td>Introduction</td> <td>The Java for loop is a control flow statement that iterates a part of the <a href="/java-programs-java-programming-examples">programs</a> multiple times. </td> <td>The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.</td> <td>The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.</td> </tr> <tr> <td>When to use</td> <td>If the number of iteration is fixed, it is recommended to use for loop.</td> <td>If the number of iteration is not fixed, it is recommended to use while loop.</td> <td>If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.</td> </tr> <tr> <td>Syntax</td> <td>for(init;condition;incr/decr){ <br> // code to be executed <br> } </td> <td> while(condition){ <br> //code to be executed <br> } </td> <td> do{ <br> //code to be executed <br> }while(condition); </td> </tr> <tr> <td>Example</td> <td> //for loop <br> for(int i=1;i<=10;i++){ <br> System.out.println(i); <br> } </=10;i++){></td> <td> //while loop <br> int i=1; <br> while(i<=10){ <br> System.out.println(i); <br> i++; <br> } </=10){></td> <td> //do-while loop <br> int i=1; <br> do{ <br> System.out.println(i); <br> i++; <br> }while(i<=10); < td> </=10);></td></tr> <tr> <td>Syntax for infinitive loop</td> <td> for(;;){ <br> //code to be executed <br> }</td> <td> while(true){ <br> //code to be executed <br> }</td> <td> do{ <br> //code to be executed <br> }while(true); </td> </tr> </table> <hr></=3;i++){></pre></=3;i++){></pre></=5;i++){></pre></=3;i++){></pre></=10;i++){>

Java Nested for Loop

Ha van egy for ciklus a másik cikluson belül, akkor ezt beágyazott for ciklusnak nevezik. A belső ciklus teljesen végrehajtódik, amikor a külső ciklus végrehajtódik.

Példa:

NestedForExample.java

 public class NestedForExample { public static void main(String[] args) { //loop of i for(int i=1;i<=3;i++){ loop of j for(int system.out.println(i+\' \'+j); } end i < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 </pre> <p> <strong>Pyramid Example 1:</strong> </p> <p> <strong>PyramidExample.java</strong> </p> <pre> public class PyramidExample { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j="1;j&lt;=i;j++){" system.out.print(\'* \'); } system.out.println(); new line < pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * </pre> <p> <strong>Pyramid Example 2:</strong> </p> <p> <strong>PyramidExample2.java</strong> </p> <pre> public class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i=i;j--){ System.out.print(&apos;* &apos;); } System.out.println();//new line } } } </pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * * * * * * * </pre> <h2>Java for-each Loop</h2> <p>The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don&apos;t need to increment value and use subscript notation.</p> <p>It works on the basis of elements and not the index. It returns element one by one in the defined variable.</p> <p> <strong>Syntax:</strong> </p> <pre> for(data_type variable : array_name){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForEachExample.java</strong> </p> <pre> //Java For-each loop example which prints the //elements of the array public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 12 23 44 56 78 </pre> <h2>Java Labeled For Loop</h2> <p>We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop.</p> <h4>Note: The break and continue keywords breaks or continues the innermost for loop respectively.</h4> <p> <strong>Syntax:</strong> </p> <pre> labelname: for(initialization; condition; increment/decrement){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>LabeledForExample.java</strong> </p> <pre> //A Java program to demonstrate the use of labeled for loop public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" break aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <p>If you use <strong>break bb;</strong> , it will break inner loop only which is the default behaviour of any loop.</p> <p> <strong>LabeledForExample2.java</strong> </p> <pre> public class LabeledForExample2 { 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){" break bb; } 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 Infinitive for Loop</h2> <p>If you use two semicolons ;; in the for loop, it will be infinitive for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(;;){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForExample.java</strong> </p> <pre> //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c </pre> <p>Now, you need to press ctrl+c to exit from the program.</p> <h2>Java for Loop vs while Loop vs do-while Loop</h2> <table class="table"> <tr> <th>Comparison</th> <th>for loop</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td>Introduction</td> <td>The Java for loop is a control flow statement that iterates a part of the <a href="/java-programs-java-programming-examples">programs</a> multiple times. </td> <td>The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.</td> <td>The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.</td> </tr> <tr> <td>When to use</td> <td>If the number of iteration is fixed, it is recommended to use for loop.</td> <td>If the number of iteration is not fixed, it is recommended to use while loop.</td> <td>If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.</td> </tr> <tr> <td>Syntax</td> <td>for(init;condition;incr/decr){ <br> // code to be executed <br> } </td> <td> while(condition){ <br> //code to be executed <br> } </td> <td> do{ <br> //code to be executed <br> }while(condition); </td> </tr> <tr> <td>Example</td> <td> //for loop <br> for(int i=1;i<=10;i++){ <br> System.out.println(i); <br> } </=10;i++){></td> <td> //while loop <br> int i=1; <br> while(i<=10){ <br> System.out.println(i); <br> i++; <br> } </=10){></td> <td> //do-while loop <br> int i=1; <br> do{ <br> System.out.println(i); <br> i++; <br> }while(i<=10); < td> </=10);></td></tr> <tr> <td>Syntax for infinitive loop</td> <td> for(;;){ <br> //code to be executed <br> }</td> <td> while(true){ <br> //code to be executed <br> }</td> <td> do{ <br> //code to be executed <br> }while(true); </td> </tr> </table> <hr></=3;i++){></pre></=3;i++){></pre></=5;i++){></pre></=3;i++){>

1. piramis példa:

PiramisPélda.java

kézi tesztelés
 public class PyramidExample { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j="1;j&lt;=i;j++){" system.out.print(\'* \'); } system.out.println(); new line < pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * </pre> <p> <strong>Pyramid Example 2:</strong> </p> <p> <strong>PyramidExample2.java</strong> </p> <pre> public class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i=i;j--){ System.out.print(&apos;* &apos;); } System.out.println();//new line } } } </pre> <p> <strong>Output:</strong> </p> <pre> * * * * * * * * * * * * * * * * * * * * * </pre> <h2>Java for-each Loop</h2> <p>The for-each loop is used to traverse array or collection in Java. It is easier to use than simple for loop because we don&apos;t need to increment value and use subscript notation.</p> <p>It works on the basis of elements and not the index. It returns element one by one in the defined variable.</p> <p> <strong>Syntax:</strong> </p> <pre> for(data_type variable : array_name){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForEachExample.java</strong> </p> <pre> //Java For-each loop example which prints the //elements of the array public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 12 23 44 56 78 </pre> <h2>Java Labeled For Loop</h2> <p>We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful while using the nested for loop as we can break/continue specific for loop.</p> <h4>Note: The break and continue keywords breaks or continues the innermost for loop respectively.</h4> <p> <strong>Syntax:</strong> </p> <pre> labelname: for(initialization; condition; increment/decrement){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>LabeledForExample.java</strong> </p> <pre> //A Java program to demonstrate the use of labeled for loop public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" break aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <p>If you use <strong>break bb;</strong> , it will break inner loop only which is the default behaviour of any loop.</p> <p> <strong>LabeledForExample2.java</strong> </p> <pre> public class LabeledForExample2 { 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){" break bb; } 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 Infinitive for Loop</h2> <p>If you use two semicolons ;; in the for loop, it will be infinitive for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(;;){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForExample.java</strong> </p> <pre> //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c </pre> <p>Now, you need to press ctrl+c to exit from the program.</p> <h2>Java for Loop vs while Loop vs do-while Loop</h2> <table class="table"> <tr> <th>Comparison</th> <th>for loop</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td>Introduction</td> <td>The Java for loop is a control flow statement that iterates a part of the <a href="/java-programs-java-programming-examples">programs</a> multiple times. </td> <td>The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.</td> <td>The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.</td> </tr> <tr> <td>When to use</td> <td>If the number of iteration is fixed, it is recommended to use for loop.</td> <td>If the number of iteration is not fixed, it is recommended to use while loop.</td> <td>If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.</td> </tr> <tr> <td>Syntax</td> <td>for(init;condition;incr/decr){ <br> // code to be executed <br> } </td> <td> while(condition){ <br> //code to be executed <br> } </td> <td> do{ <br> //code to be executed <br> }while(condition); </td> </tr> <tr> <td>Example</td> <td> //for loop <br> for(int i=1;i<=10;i++){ <br> System.out.println(i); <br> } </=10;i++){></td> <td> //while loop <br> int i=1; <br> while(i<=10){ <br> System.out.println(i); <br> i++; <br> } </=10){></td> <td> //do-while loop <br> int i=1; <br> do{ <br> System.out.println(i); <br> i++; <br> }while(i<=10); < td> </=10);></td></tr> <tr> <td>Syntax for infinitive loop</td> <td> for(;;){ <br> //code to be executed <br> }</td> <td> while(true){ <br> //code to be executed <br> }</td> <td> do{ <br> //code to be executed <br> }while(true); </td> </tr> </table> <hr></=3;i++){></pre></=3;i++){></pre></=5;i++){>

2. piramis példa:

PiramisPélda2.java

 public class PyramidExample2 { public static void main(String[] args) { int term=6; for(int i=1;i=i;j--){ System.out.print(&apos;* &apos;); } System.out.println();//new line } } } 

Kimenet:

 * * * * * * * * * * * * * * * * * * * * * 

Java for-each Loop

A for-each ciklus a Java tömb vagy gyűjtemény bejárására szolgál. Könnyebb használni, mint egyszerű for ciklust, mert nem kell növelnünk az értéket, és nem kell alsó indexet használnunk.

Elemek és nem index alapján működik. Egyesével ad vissza elemet a definiált változóban.

Szintaxis:

 for(data_type variable : array_name){ //code to be executed } 

Példa:

ForEachExample.java

 //Java For-each loop example which prints the //elements of the array public class ForEachExample { public static void main(String[] args) { //Declaring an array int arr[]={12,23,44,56,78}; //Printing array using for-each loop for(int i:arr){ System.out.println(i); } } } 
Tesztelje most

Kimenet:

 12 23 44 56 78 

Java Labeled For Loop

Minden Java for ciklusnak lehet egy neve. Ehhez használjuk a címkét a for ciklus előtt. Hasznos a beágyazott for ciklus használatakor, mivel megszakíthatjuk/folytathatjuk a ciklushoz specifikusan.

Megjegyzés: A break és a folytatás kulcsszavak rendre megtörik vagy folytatják a legbelső for ciklust.

Szintaxis:

mvc java-val
 labelname: for(initialization; condition; increment/decrement){ //code to be executed } 

Példa:

LabeledForExample.java

document.queryselector
 //A Java program to demonstrate the use of labeled for loop public class LabeledForExample { public static void main(String[] args) { //Using Label for outer and for loop aa: for(int i=1;i<=3;i++){ bb: for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" break aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <p>If you use <strong>break bb;</strong> , it will break inner loop only which is the default behaviour of any loop.</p> <p> <strong>LabeledForExample2.java</strong> </p> <pre> public class LabeledForExample2 { 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){" break bb; } 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 Infinitive for Loop</h2> <p>If you use two semicolons ;; in the for loop, it will be infinitive for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(;;){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForExample.java</strong> </p> <pre> //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c </pre> <p>Now, you need to press ctrl+c to exit from the program.</p> <h2>Java for Loop vs while Loop vs do-while Loop</h2> <table class="table"> <tr> <th>Comparison</th> <th>for loop</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td>Introduction</td> <td>The Java for loop is a control flow statement that iterates a part of the <a href="/java-programs-java-programming-examples">programs</a> multiple times. </td> <td>The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.</td> <td>The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.</td> </tr> <tr> <td>When to use</td> <td>If the number of iteration is fixed, it is recommended to use for loop.</td> <td>If the number of iteration is not fixed, it is recommended to use while loop.</td> <td>If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.</td> </tr> <tr> <td>Syntax</td> <td>for(init;condition;incr/decr){ <br> // code to be executed <br> } </td> <td> while(condition){ <br> //code to be executed <br> } </td> <td> do{ <br> //code to be executed <br> }while(condition); </td> </tr> <tr> <td>Example</td> <td> //for loop <br> for(int i=1;i<=10;i++){ <br> System.out.println(i); <br> } </=10;i++){></td> <td> //while loop <br> int i=1; <br> while(i<=10){ <br> System.out.println(i); <br> i++; <br> } </=10){></td> <td> //do-while loop <br> int i=1; <br> do{ <br> System.out.println(i); <br> i++; <br> }while(i<=10); < td> </=10);></td></tr> <tr> <td>Syntax for infinitive loop</td> <td> for(;;){ <br> //code to be executed <br> }</td> <td> while(true){ <br> //code to be executed <br> }</td> <td> do{ <br> //code to be executed <br> }while(true); </td> </tr> </table> <hr></=3;i++){></pre></=3;i++){>

Ha használ szünet bb; , csak a belső hurkot szakítja meg, ami minden ciklus alapértelmezett viselkedése.

LabeledForExample2.java

 public class LabeledForExample2 { 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){" break bb; } 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 Infinitive for Loop</h2> <p>If you use two semicolons ;; in the for loop, it will be infinitive for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(;;){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>ForExample.java</strong> </p> <pre> //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c </pre> <p>Now, you need to press ctrl+c to exit from the program.</p> <h2>Java for Loop vs while Loop vs do-while Loop</h2> <table class="table"> <tr> <th>Comparison</th> <th>for loop</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td>Introduction</td> <td>The Java for loop is a control flow statement that iterates a part of the <a href="/java-programs-java-programming-examples">programs</a> multiple times. </td> <td>The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.</td> <td>The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition.</td> </tr> <tr> <td>When to use</td> <td>If the number of iteration is fixed, it is recommended to use for loop.</td> <td>If the number of iteration is not fixed, it is recommended to use while loop.</td> <td>If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop.</td> </tr> <tr> <td>Syntax</td> <td>for(init;condition;incr/decr){ <br> // code to be executed <br> } </td> <td> while(condition){ <br> //code to be executed <br> } </td> <td> do{ <br> //code to be executed <br> }while(condition); </td> </tr> <tr> <td>Example</td> <td> //for loop <br> for(int i=1;i<=10;i++){ <br> System.out.println(i); <br> } </=10;i++){></td> <td> //while loop <br> int i=1; <br> while(i<=10){ <br> System.out.println(i); <br> i++; <br> } </=10){></td> <td> //do-while loop <br> int i=1; <br> do{ <br> System.out.println(i); <br> i++; <br> }while(i<=10); < td> </=10);></td></tr> <tr> <td>Syntax for infinitive loop</td> <td> for(;;){ <br> //code to be executed <br> }</td> <td> while(true){ <br> //code to be executed <br> }</td> <td> do{ <br> //code to be executed <br> }while(true); </td> </tr> </table> <hr></=3;i++){>

Java Infinitive for Loop

Ha két pontosvesszőt használ ;; a for ciklusban infinitív for ciklus lesz.

Szintaxis:

 for(;;){ //code to be executed } 

Példa:

Például.java

 //Java program to demonstrate the use of infinite for loop //which prints an statement public class ForExample { public static void main(String[] args) { //Using no condition in for loop for(;;){ System.out.println(&apos;infinitive loop&apos;); } } } 

Kimenet:

 infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c 

Most meg kell nyomnia a ctrl+c billentyűkombinációt a programból való kilépéshez.

Java for Loop vs while Loop vs do-while Loop

Összehasonlítás hurokhoz míg hurok do-while ciklus
Bevezetés A Java for loop egy vezérlőfolyamat-utasítás, amely a folyamat egy részét iterálja programokat többször. A Java while ciklus egy vezérlőfolyamat, amely adott logikai feltétel alapján ismételten végrehajtja a programok egy részét. A Java do while ciklus egy vezérlőfolyamat, amely legalább egyszer végrehajtja a programok egy részét, és a további végrehajtás az adott logikai feltételtől függ.
Mikor kell használni Ha az iterációk száma rögzített, akkor a for ciklus használata javasolt. Ha az iterációk száma nem rögzített, akkor a while ciklus használata javasolt. Ha az iterációk száma nem rögzített, és a ciklust legalább egyszer végre kell hajtani, akkor javasolt a do-while ciklus használata.
Szintaxis for(init;condition;incr/decr){
// végrehajtandó kód
}
while(feltétel){
// végrehajtandó kód
}
do{
// végrehajtandó kód
}while(feltétel);
Példa //for ciklus
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while ciklus
int i=1;
miközben én<=10){
System.out.println(i);
i++;
}
//do-while ciklus
int i=1;
do{
System.out.println(i);
i++;
}miközben én<=10); < td>
Szintaxis az infinitív ciklushoz for(;;){
// végrehajtandó kód
}
while(igaz){
// végrehajtandó kód
}
do{
// végrehajtandó kód
}while(true);