logo

Laços em Java

O Java para loop é usado para iterar uma parte do programa várias vezes. Se o número de iterações for fixo , é recomendado usar o loop for.

Existem três tipos de loops for em Java.

Laços em Java
  • Simples para Loop
  • Para cada ou aprimorado para loop
  • Rotulado para Loop

Java Simples para Loop

Um loop for simples é o mesmo que C / C++ . Podemos inicializar o variável , verifique a condição e o valor de incremento/decremento. Consiste em quatro partes:

    Inicialização: É a condição inicial que é executada uma vez quando o loop começa. Aqui podemos inicializar a variável ou podemos usar uma variável já inicializada. É uma condição opcional.Doença: É a segunda condição executada a cada vez para testar a condição do loop. Ele continua a execução até que a condição seja falsa. Deve retornar um valor booleano verdadeiro ou falso. É uma condição opcional.Incremento/Decremento: aumenta ou diminui o valor da variável. É uma condição opcional.Declaração: A instrução do loop é executada todas as vezes até que a segunda condição seja falsa.

Sintaxe:

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

Fluxograma:

for loop no fluxograma java

Exemplo:

PorExemplo.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 aninhado para loop

Se tivermos um loop for dentro de outro loop, ele será conhecido como loop for aninhado. O loop interno é executado completamente sempre que o loop externo é executado.

Exemplo:

desativar o modo de desenvolvedor

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

Exemplo de pirâmide 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++){>

Exemplo de pirâmide 2:

PirâmideExample2.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 } } } 

Saída:

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

Java para cada loop

O loop for-each é usado para percorrer array ou coleção em Java. É mais fácil de usar do que o simples loop for porque não precisamos incrementar o valor e usar a notação de subscrito.

Funciona com base em elementos e não no índice. Ele retorna o elemento um por um na variável definida.

Sintaxe:

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

Exemplo:

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

Saída:

 12 23 44 56 78 

Java rotulado para loop

Podemos ter um nome para cada loop for Java. Para fazer isso, usamos rótulo antes do loop for. É útil ao usar o loop for aninhado, pois podemos interromper/continuar o loop for específico.

Nota: As palavras-chave break e continue quebram ou continuam o loop for mais interno, respectivamente.

Sintaxe:

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

Exemplo:

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

Se você usar quebrar bb; , ele interromperá apenas o loop interno, que é o comportamento padrão de qualquer loop.

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 Infinitivo para Loop

Se você usar dois ponto e vírgula ;; no loop for, será loop for infinitivo.

Sintaxe:

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

Exemplo:

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

Saída:

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

Agora, você precisa pressionar ctrl+c para sair do programa.

Java for Loop versus while Loop versus do-while Loop

Comparação para loop enquanto loop loop do-while
Introdução O loop for Java é uma instrução de fluxo de controle que itera uma parte do programas várias vezes. O loop while Java é uma instrução de fluxo de controle que executa uma parte dos programas repetidamente com base em uma determinada condição booleana. O loop do while Java é uma instrução de fluxo de controle que executa uma parte dos programas pelo menos uma vez e a execução adicional depende da condição booleana fornecida.
Quando usar Se o número de iterações for fixo, é recomendado usar o loop for. Se o número de iterações não for fixo, é recomendado usar o loop while. Se o número de iterações não for fixo e você precisar executar o loop pelo menos uma vez, é recomendado usar o loop do-while.
Sintaxe for(init;condição;incr/decr){
//código a ser executado
}
enquanto(condição){
//código a ser executado
}
fazer{
//código a ser executado
}enquanto(condição);
Exemplo //para loop
para(int i=1;i<=10;i++){
System.out.println(i);
}
//loop while
int i=1;
enquanto eu<=10){
System.out.println(i);
eu++;
}
//loop do-while
int i=1;
fazer{
System.out.println(i);
eu++;
}enquanto eu<=10); < td>
Sintaxe para loop infinitivo para(;;){
//código a ser executado
}
enquanto(verdadeiro){
//código a ser executado
}
fazer{
//código a ser executado
}enquanto(verdadeiro);