logo

Silmukat Javassa

Java silmukalle käytetään toistamaan ohjelman osaa useita kertoja. Jos iteraatioiden määrä on korjattu , on suositeltavaa käyttää silmukalle.

Javassa on kolmenlaisia ​​for-silmukoita.

Silmukat Javassa
  • Yksinkertainen Loopille
  • Jokaiselle tai Enhanced for Loop
  • Merkitty silmukalle

Java Simple for Loop

Yksinkertainen for-silmukka on sama kuin C / C++ . Voimme alustaa muuttuja , tarkista kunto ja lisäys/vähennysarvo. Se koostuu neljästä osasta:

    Alustus: Se on alkuehto, joka suoritetaan kerran silmukan alkaessa. Täällä voimme alustaa muuttujan tai voimme käyttää jo alustettua muuttujaa. Se on valinnainen ehto.Kunto: Se on toinen ehto, joka suoritetaan joka kerta silmukan tilan testaamiseksi. Se jatkaa suoritusta, kunnes ehto on epätosi. Sen on palautettava looginen arvo joko tosi tai epätosi. Se on valinnainen ehto.Lisäys/vähennys: Se lisää tai vähentää muuttujan arvoa. Se on valinnainen ehto.lausunto: Silmukan lausetta suoritetaan joka kerta, kunnes toinen ehto on epätosi.

Syntaksi:

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

Vuokaavio:

silmukalle Java-vuokaaviossa

Esimerkki:

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

Jos meillä on for-silmukka toisen silmukan sisällä, sitä kutsutaan sisäkkäiseksi silmukaksi. Sisäinen silmukka suoritetaan kokonaan aina, kun ulompi silmukka suoritetaan.

Esimerkki:

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

Pyramidi esimerkki 1:

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

Pyramidi esimerkki 2:

PyramidEsimerkki2.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 } } } 

Lähtö:

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

Java for-each Loop

For-each-silmukkaa käytetään taulukon tai kokoelman läpikulkuun Javassa. Sitä on helpompi käyttää kuin simple for loop, koska meidän ei tarvitse lisätä arvoa ja käyttää alaindeksimerkintää.

Se toimii elementtien eikä indeksin perusteella. Se palauttaa elementin yksitellen määritellyssä muuttujassa.

Syntaksi:

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

Esimerkki:

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); } } } 
Testaa nyt

Lähtö:

 12 23 44 56 78 

Java-merkintä For Loop

Meillä voi olla nimi jokaiselle Java for -silmukalle. Käytämme tätä varten etikettiä ennen for-silmukkaa. Se on hyödyllinen käytettäessä sisäkkäistä silmukkaa, koska voimme katkaista/jatkaa silmukkaa varten.

Huomautus: Katkaise- ja jatka-avainsanat katkaisevat tai jatkavat sisimmän for-silmukan vastaavasti.

Syntaksi:

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

Esimerkki:

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

Jos käytät tauko bb; , se katkaisee vain sisäisen silmukan, mikä on minkä tahansa silmukan oletuskäyttäytyminen.

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

Jos käytät kahta puolipistettä ;; for-silmukassa se on infinitiivi silmukalle.

Syntaksi:

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

Esimerkki:

polymorfismi javassa

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

Lähtö:

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

Nyt sinun on painettava ctrl+c poistuaksesi ohjelmasta.

Java for Loop vs while Loop vs do-while Loop

Vertailu silmukalle kun silmukka do-while -silmukka
Johdanto Java for loop on ohjausvuon käsky, joka iteroi osan silmukasta ohjelmia useita kertoja. Java while -silmukka on ohjausvuolauseke, joka suorittaa osan ohjelmista toistuvasti tietyn loogisen ehdon perusteella. Java do while -silmukka on ohjausvuon käsky, joka suorittaa osan ohjelmista vähintään kerran ja jatkosuoritus riippuu annetusta boolen ehtosta.
Milloin käyttää Jos iteraatioiden lukumäärä on kiinteä, on suositeltavaa käyttää silmukkaa. Jos iteraatioiden lukumäärä ei ole kiinteä, on suositeltavaa käyttää while-silmukkaa. Jos iteraatioiden lukumäärä ei ole kiinteä ja silmukka on suoritettava vähintään kerran, on suositeltavaa käyttää do-while -silmukkaa.
Syntaksi for(init;condition;incr/decr){
// Suoritettava koodi
}
while(ehto){
//suoritettava koodi
}
tehdä{
//suoritettava koodi
}while(ehto);
Esimerkki //silmukalle
for(int i=1;i<=10;i++){
System.out.println(i);
}
//while loop
int i = 1;
sillä aikaa kun minä<=10){
System.out.println(i);
i++;
}
//do-while -silmukka
int i = 1;
tehdä{
System.out.println(i);
i++;
}sillä aikaa kun minä<=10); < td>
Syntaksi infinitiivisilmukalle for(;;){
//suoritettava koodi
}
while(true){
//suoritettava koodi
}
tehdä{
//suoritettava koodi
}while(true);