logo

Sløjfer i Java

Java for sløjfe bruges til at gentage en del af programmet flere gange. Hvis antallet af iterationer er fast , anbefales det at bruge til loop.

Der er tre typer for loops i Java.

Sløjfer i Java
  • Enkel til Loop
  • For hver eller Enhanced for Loop
  • Mærket til Loop

Java Simple for Loop

En simpel for loop er den samme som C / C++ . Vi kan initialisere variabel , tjek tilstand og stigning/mindsk værdi. Den består af fire dele:

a-b beskæring
    Initialisering: Det er startbetingelsen, som udføres én gang, når løkken starter. Her kan vi initialisere variablen, eller vi kan bruge en allerede initialiseret variabel. Det er en valgfri betingelse.Tilstand: Det er den anden betingelse, som udføres hver gang for at teste sløjfens tilstand. Den fortsætter med eksekveringen, indtil betingelsen er falsk. Den skal returnere boolsk værdi enten sand eller falsk. Det er en valgfri betingelse.Øge/reducere: Den øger eller formindsker variabelværdien. Det er en valgfri betingelse.Udmelding: Løkkens sætning udføres hver gang, indtil den anden betingelse er falsk.

Syntaks:

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

Flowchart:

for loop i java flowchart

Eksempel:

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

Hvis vi har en for-løkke inde i den anden løkke, er den kendt som indlejret for løkke. Den indre løkke udføres fuldstændigt, når den ydre løkke udføres.

Eksempel:

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

Pyramideeksempel 1:

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

Pyramideeksempel 2:

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

Produktion:

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

Java for hver sløjfe

For-each-løkken bruges til at krydse array eller samling i Java. Det er nemmere at bruge end simpelt for loop, fordi vi ikke behøver at øge værdien og bruge subscript notation.

Det fungerer på basis af elementer og ikke indekset. Det returnerer element et efter et i den definerede variabel.

Syntaks:

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

Eksempel:

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); } } } 
Test det nu

Produktion:

 12 23 44 56 78 

Java mærket for loop

Vi kan have et navn på hver Java for loop. For at gøre det bruger vi label før for-løkken. Det er nyttigt, mens du bruger den indlejrede for loop, da vi kan bryde/fortsætte specifikt for loop.

javascript kommentar

Bemærk: Nøgleordene pause og fortsæt bryder eller fortsætter henholdsvis det inderste for loop.

Syntaks:

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

Eksempel:

LabeledForExample.java

lav et sh-script eksekverbart
 //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++){>

Hvis du bruger bryde bb; , vil det kun bryde den indre løkke, hvilket er standardadfærden for enhver løkke.

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

Hvis du bruger to semikolon ;; i for-løkken vil det være infinitiv for loop.

Syntaks:

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

Eksempel:

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

Produktion:

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

Nu skal du trykke på ctrl+c for at afslutte programmet.

Java for Loop vs mens Loop vs do-while Loop

Sammenligning for sløjfe mens loop gør-mens-løkke
Introduktion Java for loop er en kontrolflow-sætning, der itererer en del af programmer flere gange. Java while-løkken er en kontrolflow-sætning, der udfører en del af programmerne gentagne gange på basis af en given boolesk tilstand. Java do while-løkken er en kontrolflow-sætning, der udfører en del af programmerne mindst én gang, og den videre udførelse afhænger af den givne booleske tilstand.
Hvornår skal bruges Hvis antallet af iterationer er fast, anbefales det at bruge til loop. Hvis antallet af iterationer ikke er fast, anbefales det at bruge while loop. Hvis antallet af iterationer ikke er fast, og du skal udføre loopet mindst én gang, anbefales det at bruge do-while loop.
Syntaks for(init;condition;incr/decr){
// kode, der skal udføres
}
while(tilstand){
//kode, der skal udføres
}
gør{
//kode, der skal udføres
}mens(tilstand);
Eksempel //for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
//mens loop
int i=1;
mens jeg<=10){
System.out.println(i);
i++;
}
//do-while loop
int i=1;
gør{
System.out.println(i);
i++;
}mens jeg<=10); < td>
Syntaks for infinitiv loop til(;;){
//kode, der skal udføres
}
while(true){
//kode, der skal udføres
}
gør{
//kode, der skal udføres
}mens(sand);