logo

Java Break Statement

När en break-sats påträffas i en loop, avslutas slingan omedelbart och programkontrollen återupptas vid nästa sats som följer efter slingan.

Java ha sönder uttalande används för att bryta loop eller växla påstående. Den bryter programmets nuvarande flöde vid specificerat tillstånd. Vid inre slinga bryter den endast inre slinga.

länkad lista i java

Vi kan använda Java break statement i alla typer av loopar som t.ex för slinga , medan loop och gör-under loop .

Syntax:

 jump-statement; break; 

Flödesschema över avbrottsuttalande

java break statement flödesschema

Java Break Statement med Loop

Exempel:

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

Java Break Statement med Inner Loop

Den bryter bara den inre slingan om du använder break-satsen inuti den inre slingan.

Exempel:

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

Java Break Statement med märkt For Loop

Vi kan använda break statement med en etikett. Funktionen har introducerats sedan JDK 1.5. Så vi kan bryta vilken loop som helst i Java nu oavsett om den är yttre eller inre loop.

Exempel:

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

Java Break Statement i while loop

Exempel:

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

Java Break Statement i do-while loop

Exempel:

BreakDoWhileExample.java

ett exempel på ett OS med öppen källkod är
 //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);>

Java Break Statement med Switch

För att förstå exemplet på break with switch-sats, besök här: Java Switch Statement .