logo

Slučky v jazyku Java

Java pre slučku sa používa na niekoľkonásobné opakovanie časti programu. Ak je počet iterácií pevné , odporúča sa použiť pre slučku.

V Jave existujú tri typy cyklov for.

Slučky v jazyku Java
  • Jednoduché pre Loop
  • Pre každý alebo Enhanced for Loop
  • Označené ako Loop

Java Simple for Loop

Jednoduchý cyklus for je rovnaký ako C / C++ . Môžeme inicializovať premenlivý , skontrolujte stav a zvýšte/znížte hodnotu. Skladá sa zo štyroch častí:

    Inicializácia: Je to počiatočná podmienka, ktorá sa vykoná raz pri spustení cyklu. Tu môžeme inicializovať premennú, alebo môžeme použiť už inicializovanú premennú. Je to voliteľná podmienka.Podmienka: Je to druhá podmienka, ktorá sa vykoná zakaždým, aby sa otestovala podmienka cyklu. Pokračuje vo vykonávaní, kým podmienka nie je nepravdivá. Musí vrátiť boolovskú hodnotu buď true alebo false. Je to voliteľná podmienka.Zvýšenie/zníženie: Zvyšuje alebo znižuje hodnotu premennej. Je to voliteľná podmienka.Vyhlásenie: Príkaz cyklu sa vykoná zakaždým, kým druhá podmienka nie je nepravdivá.

Syntax:

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

Vývojový diagram:

pre slučku vo vývojovom diagrame java

Príklad:

ForExample.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

Ak máme slučku for vo vnútri inej slučky, je známa ako vnorená slučka for. Vnútorná slučka sa úplne vykoná vždy, keď sa vykoná vonkajšia slučka.

Príklad:

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

Príklad 1 pyramídy:

PyramidExample.java

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

Príklad pyramídy 2:

Príklad pyramídy2.java

počítačové siete
 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 } } } 

Výkon:

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

Java pre každú slučku

Slučka for-each sa používa na prechádzanie poľa alebo kolekcie v jazyku Java. Je to jednoduchšie na použitie ako jednoduchý cyklus for, pretože nepotrebujeme zvyšovať hodnotu a používať dolný index.

Funguje na základe prvkov a nie indexu. Vráti prvok jeden po druhom v definovanej premennej.

Syntax:

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

Príklad:

ForEachExample.java

vytvorenie poľa reťazcov v jazyku 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); } } } 
Vyskúšajte to

Výkon:

 12 23 44 56 78 

Java Labeled For Loop

Môžeme mať názov každého cyklu Java for. Aby sme to dosiahli, používame označenie pred cyklom for. Je to užitočné pri používaní vnoreného cyklu for, pretože môžeme prerušiť/pokračovať špecifický cyklus for.

Poznámka: Kľúčové slová break a continue prerušia alebo pokračujú najvnútornejšiu slučku for.

Syntax:

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

Príklad:

LabeledForExample.java

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

Ak používate prestávka bb; , preruší iba vnútornú slučku, čo je predvolené správanie každej slučky.

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

Ak použijete dve bodkočiarky ;; v slučke for bude infinitívna slučka for.

Syntax:

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

Príklad:

ForExample.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;); } } } 

Výkon:

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

Teraz musíte stlačiť ctrl+c na ukončenie programu.

Java for Loop vs while Loop vs do-while Loop

Porovnanie pre slučku pričom slučka slučka do-while
Úvod Slučka Java for je príkaz riadiaceho toku, ktorý iteruje časť súboru programy viac ráz. Slučka Java while je príkazom riadiaceho toku, ktorý opakovane vykonáva časť programov na základe danej boolovskej podmienky. Slučka Java do while je príkaz riadiaceho toku, ktorý vykoná časť programov aspoň raz a ďalšie vykonávanie závisí od danej boolovskej podmienky.
Kedy použiť Ak je počet iterácií pevný, odporúča sa použiť cyklus for. Ak počet iterácií nie je pevný, odporúča sa použiť cyklus while. Ak počet iterácií nie je pevný a musíte vykonať cyklus aspoň raz, odporúča sa použiť cyklus do-while.
Syntax for(init;condition;incr/decr){
// kód, ktorý sa má vykonať
}
while(condition){
//kód, ktorý sa má vykonať
}
urobiť{
//kód, ktorý sa má vykonať
}pričom(podmienka);
Príklad //pre slučku
for(int i=1;i<=10;i++){
System.out.println(i);
}
//cyklus while
int i=1;
kým<=10){
System.out.println(i);
i++;
}
//do-while cyklus
int i=1;
urobiť{
System.out.println(i);
i++;
}kým<=10); < td>
Syntax pre infinitívnu slučku pre(;;){
//kód, ktorý sa má vykonať
}
while(true){
//kód, ktorý sa má vykonať
}
urobiť{
//kód, ktorý sa má vykonať
}pričom(pravda);