logo

Instrução de quebra Java

Quando uma instrução break é encontrada dentro de um loop, o loop é imediatamente encerrado e o controle do programa é retomado na próxima instrução após o loop.

O Java quebrar instrução é usada para quebrar loop ou trocar declaração. Ele interrompe o fluxo atual do programa na condição especificada. No caso de loop interno, ele quebra apenas o loop interno.

ligue o java

Podemos usar a instrução Java break em todos os tipos de loops, como para loop , enquanto loop e loop do-while .

Sintaxe:

 jump-statement; break; 

Fluxograma da Declaração de Quebra

fluxograma de instrução break java

Instrução Java Break com Loop

Exemplo:

BreakExample.java

 //Java Program to demonstrate the use of break statement //inside the for loop. public class BreakExample { public static void main(String[] args) { //using for loop for(int i=1;i<=10;i++){ if(i="=5){" breaking the loop break; } system.out.println(i); < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Inner Loop</h2> <p>It breaks inner loop only if you use break statement inside the inner loop.</p> <p> <strong>Example:</strong> </p> <p> <strong>BreakExample2.java</strong> </p> <pre> //Java Program to illustrate the use of break statement //inside an inner loop public class BreakExample2 { public static void main(String[] args) { //outer loop for(int i=1;i<=3;i++){ inner loop for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using break statement inside the break; } 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 Break Statement with Labeled For Loop</h2> <p>We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.</p> <p> <strong>Example:</strong> </p> <p> <strong>BreakExample3.java</strong> </p> <pre> //Java Program to illustrate the use of continue statement //with label inside an inner loop to break outer loop public class BreakExample3 { 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){" using break statement with label aa; } system.out.println(i+' '+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <h2>Java Break Statement in while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the while loop. public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i="=5){" using break statement i++; break; it will the loop } system.out.println(i); < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement in do-while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakDoWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Switch</h2> <p>To understand the example of break with switch statement, please visit here: <a href="/java-switch-statement">Java Switch Statement</a> .</p> <hr></=10);></pre></=10){></pre></=3;i++){></pre></=3;i++){></pre></=10;i++){>

Instrução Java Break com Loop Interno

Ele quebra o loop interno somente se você usar a instrução break dentro do loop interno.

Exemplo:

BreakExample2.java

 //Java Program to illustrate the use of break statement //inside an inner loop public class BreakExample2 { public static void main(String[] args) { //outer loop for(int i=1;i<=3;i++){ inner loop for(int j="1;j&lt;=3;j++){" if(i="=2&amp;&amp;j==2){" using break statement inside the break; } 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 Break Statement with Labeled For Loop</h2> <p>We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.</p> <p> <strong>Example:</strong> </p> <p> <strong>BreakExample3.java</strong> </p> <pre> //Java Program to illustrate the use of continue statement //with label inside an inner loop to break outer loop public class BreakExample3 { 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){" using break statement with label aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <h2>Java Break Statement in while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the while loop. public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i="=5){" using break statement i++; break; it will the loop } system.out.println(i); < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement in do-while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakDoWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Switch</h2> <p>To understand the example of break with switch statement, please visit here: <a href="/java-switch-statement">Java Switch Statement</a> .</p> <hr></=10);></pre></=10){></pre></=3;i++){></pre></=3;i++){>

Instrução Java Break com Rotulado For Loop

Podemos usar a instrução break com um rótulo. O recurso foi introduzido desde o JDK 1.5. Portanto, podemos quebrar qualquer loop em Java agora, seja ele externo ou interno.

Exemplo:

BreakExample3.java

 //Java Program to illustrate the use of continue statement //with label inside an inner loop to break outer loop public class BreakExample3 { 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){" using break statement with label aa; } system.out.println(i+\' \'+j); < pre> <p> <strong>Output:</strong> </p> <pre> 1 1 1 2 1 3 2 1 </pre> <h2>Java Break Statement in while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the while loop. public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i="=5){" using break statement i++; break; it will the loop } system.out.println(i); < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement in do-while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakDoWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Switch</h2> <p>To understand the example of break with switch statement, please visit here: <a href="/java-switch-statement">Java Switch Statement</a> .</p> <hr></=10);></pre></=10){></pre></=3;i++){>

Instrução Java Break no loop while

Exemplo:

BreakWhileExample.java

 //Java Program to demonstrate the use of break statement //inside the while loop. public class BreakWhileExample { public static void main(String[] args) { //while loop int i=1; while(i<=10){ if(i="=5){" using break statement i++; break; it will the loop } system.out.println(i); < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement in do-while loop</h2> <p> <strong>Example:</strong> </p> <p> <strong>BreakDoWhileExample.java</strong> </p> <pre> //Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Switch</h2> <p>To understand the example of break with switch statement, please visit here: <a href="/java-switch-statement">Java Switch Statement</a> .</p> <hr></=10);></pre></=10){>

Instrução Java Break no loop do-while

Exemplo:

BreakDoWhileExample.java

sorriso mais bonito do mundo
 //Java Program to demonstrate the use of break statement //inside the Java do-while loop. public class BreakDoWhileExample { public static void main(String[] args) { //declaring variable int i=1; //do-while loop do{ if(i==5){ //using break statement i++; break;//it will break the loop } System.out.println(i); i++; }while(i<=10); } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 </pre> <h2>Java Break Statement with Switch</h2> <p>To understand the example of break with switch statement, please visit here: <a href="/java-switch-statement">Java Switch Statement</a> .</p> <hr></=10);>

Instrução Java Break com Switch

Para entender o exemplo de break com instrução switch, visite aqui: Declaração de mudança Java .