Heim > Java > javaLernprogramm > Break-Anweisung in Java

Break-Anweisung in Java

PHPz
Freigeben: 2024-08-30 15:24:02
Original
446 Leute haben es durchsucht

The break statement is a keyword to terminate the execution of the instructions in the loop; code execution remains to continue after the loop. The break statement is one of the best ways to terminate the execution in the loop. Break statement in java can be used in multiple use cases; the best use case of the break statement comes in the loop where it terminates the execution of code inside the loop & code execution resumes after the loop. Another example of a break statement is in the switch case.

ADVERTISEMENT Popular Course in this category FINANCIAL MODELING & VALUATION - Specialization | 51 Course Series | 30 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Example:

switch(value) {
case 1:
// Statements
break;
case 2:
// Statements
break;
case 3:
// Statements
break;
default :
//Statements
}
Nach dem Login kopieren

Syntax:

The break statement is a simple one-line statement as given below. In the above-given switch case syntax, we can see how the break is placed at the end of each case to terminate execution inside of the loop.

break;
Nach dem Login kopieren

Flow Diagram

In the below-given diagram, we can see how to break the statement terminates the execution inside the loop/switch statement & execution jumps to the next step after the loop.

Break-Anweisung in Java

 

How does Break Statement work in Java?

  • Break statement jumps the code compilation to the outside of the loop, while loop, do-while, for-each, switch, & other conditional statements.
  • The break statement closes the loop where it is placed. If a break statement is not labeled and placed inside the loop, it will jump code execution outside of that loop.
  • In the labeled loop, the break statement is placed with the label. Break statement with label jumps program execution control to outside of that labeled code block.
  • In Java, the break statement also works the same as in other programming languages.

Example #1

See the example of how execution is being terminated after successful execution of if statement & program execution control moves to the next step.

public class BreakStatementExample{
public static void main(String[] args){
// Create frame with title Registration Demo
int i=1;
while(i < 10){
if(i == 5){
break;
}
// Printing the counter
System.out.print(i);
System.out.print("\n");
i++;
}
}
}
Nach dem Login kopieren

In the above-given example, In the loop, once the counter reaches up to 5, then the break statement executes. Once the break statement executes, the further counter will not display as the break statement made execution to out of the loop. We can see the expected result in the attached screenshot below.

Output:

Break-Anweisung in Java

Example #2

In this example, we can see how execution jumps to the outside of the switch case statement once any case gets true.

public class BreakStatementSwitchCaseExample{
public static void main(String[] args){
int y=2;
showExecution(y);
}
public static void showExecution(int i){
switch(i){
case 1:
System.out.print("Case 1 Executed");
break;
case 2:
System.out.print("Case 2 Executed");
break;
case 3:
System.out.print("Case 1 Executed");
break;
default:
System.out.print("Default Case Executed");
}
}
}
Nach dem Login kopieren

In the above-given example break statement is placed in each case of the switch statement. Once any case code block executed, break statement stop execution at that point & resume the code execution outside of the switch statement.

Output:

Break-Anweisung in Java

Example #3

In this example, the break statement is placed inside the inner loop. Whenever condition met & break statement executes, code execution jumps program execution control to outside of the inner loop.

public class LabeledBreakStatement{
public static void main(String[] args){
for(int i=1; i < 3; i++){
for(int j=1; j < 4; j++){
System.out.print(i +"  "+ j);
System.out.print("\n");
if(j == 2){
break;
}
}
}
}
}
Nach dem Login kopieren

In the below-given screenshot, how the inner loop is terminated after printing 2.

Output:

Break-Anweisung in Java

Example #4

In the below-given example, we can see a labeled break statement example. Labeled break refers to a particular code block. When the labeled break statement executes execution, controls move out of the labeled code block.

public class LabeledBreakStatement{
public static void main(String[] args){
primary:
for(int i=1; i < 3; i++){
secondary:
for(int j=1; j < 4; j++){
System.out.print(i +"  "+ j);
System.out.print("\n");
if(j == 2){
break primary;
}
}
}
}
}
Nach dem Login kopieren

In the above-given example, each loop starts after a label. In certain conditions, the break statement is terminating that labeled loop. & execution control moves to the outside of the labeled. We can see the output of the program in the below-given screenshot.

Output:

Break-Anweisung in Java

Example #5

Moving to the next example, This example has the example of a do-while statement & also labeled break statement in it.

public class DoWhileBreakStatement{
public static void main(String[] args){
int j = 1;
do{
if(j == 6){
break;
}
System.out.println(j);
j++;
}
while(j < 9);
System.out.println("\n\n");
labeledBreakLoopExample();
//Further Checking execution moves to this step or not
}
//Labeled statement with nested loop
public static void labeledBreakLoopExample(){
primary:
for(int i=1; i < 3; i++){
secondary:
for(int j=1; j < 4; j++){
System.out.print(i +"  "+ j);
System.out.print("\n");
if(j == 2){
break secondary;
}
}
}
}
}
Nach dem Login kopieren

In the above-given example, we can see that the method will print the first counter up to 5. After execution of break statement when the counter reaches 6, program execution control executes the if case & entered into the if case inside code. Break statement execution inside of the if condition terminates the execution of loop & moves execution control to the next step.

Weitere nächste Methode wird ausgeführt. Diese Methode verfügt über beschriftete Anweisungen. Diese Methode führt die Break-Anweisung mit einem sekundären Label unter bestimmten wahren Bedingungen aus, wodurch die Ausführung einer sekundären Label-Schleife geschlossen wird und die Programmausführung außerhalb der internen Label-Schleife springt.

Ausgabe:

Break-Anweisung in Java

Vorteile

  • Eine Schleife kann einfach mithilfe einer break-Anweisung geschlossen werden.
  • Es kann in jeder Art von Schleife wie for, foreach, while, do-while usw. verwendet werden.
  • Die break-Anweisung kann mit einer beschrifteten Schleife verwendet werden. Die beschriftete break-Anweisung kann verwendet werden, um die Ausführung dieser beschrifteten Schleife weiter zu beenden. Diese beschriftete Schleife kann sich innerhalb oder an der obersten Beschriftung in den Anweisungen befinden.
  • Break-Anweisung macht die Schleife flexibler und verleiht ihr mehr Leistung.

Fazit – Break-Anweisung in Java

Break ist das reservierte Java-Schlüsselwort. Es ist eines der Schlüsselwörter, die von Programmierern verwendet werden, um die Ausführung in der Schleife oder in bedingten Anweisungen sofort zu beenden und die Programmausführungssteuerung zum nächsten Schritt zu bewegen. Sein besseres Anwendungsszenario fällt unter die Entscheidungsaussagen.

Das obige ist der detaillierte Inhalt vonBreak-Anweisung in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage