logo

Изявление за прекъсване на Java

Когато се срещне оператор за прекъсване в цикъл, цикълът незабавно се прекратява и контролът на програмата се възобновява при следващия оператор след цикъла.

Java прекъсвам изразът се използва за прекъсване на цикъл или превключвател изявление. Той прекъсва текущия поток на програмата при определено състояние. В случай на вътрешен цикъл прекъсва само вътрешния цикъл.

скрипт за зареждане на javascript

Можем да използваме оператор за прекъсване на Java във всички видове цикли, като напр за цикъл , докато цикъл и do-while цикъл .

Синтаксис:

 jump-statement; break; 

Блок-схема на израза Break

блок-схема на изявление за прекъсване на java

Изявление за прекъсване на Java с цикъл

Пример:

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 вътре във вътрешния цикъл.

Пример:

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 с етикет For Loop

Можем да използваме оператор break с етикет. Функцията е въведена от JDK 1.5. Така че сега можем да прекъснем всеки цикъл в Java, независимо дали е външен или вътрешен цикъл.

Пример:

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 в цикъла while

Пример:

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 в do-while цикъл

Пример:

BreakDoWhileExample.java

powershell коментар многоредов
 //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 с Switch

За да разберете примера за оператор прекъсване с превключване, моля, посетете тук: Java Switch Statement .