Java Break Keyword
Break:
- The word Break is a keyword or a statement in a java programming language.
- This Keyword break is used to stop the execution of the loop or switch statements etc.
- The loop is stopped by the break statement, so it is mandatory to end the loop.
- When a break statement is approaches a loop, the reputation of the loop terminates and starts backward from the loop to the statement of the starting loop.
- When we are not sure about the reputations of the loop then basically we use the break statement.
- Break keyword controls the Structures and switch statements in the loop. loops and switch statements in java are terminated by using break keyword
- When the loop encounters the break word within a loop, the loop gets terminated and the controls of the program shift to the next statement it follows the loop
- The inner loop will stop execution or end the program or terminate the loop only. When a break keyword implemented in a nested loop, the inner loop will be stop executed.
- When few rules are met then it is used to stop the statements.
- In the switch statement the break key word has a specific role.
- Break statement is used to break the cases in the switch statement to stop the execution at case, it wouldn’t execute the next cases subsequently if the controls of the program are transferred to another case.
- The dominion of the program displaces to the next part of the program by executethe break statement.
- The break statement transfers program controls to the next statements of the program it can be called as program execution controller.
Syntax
For break statement in Java the syntax is:
Break;
Break Statement Flow Chart
Break Statements in Loops
Example:
BreakExample.java
//to understand the implementation of break keyword in java programming language
//usage of break keyword in for loop.
public class BreakEx {
public static void main (String [] s) {
//for loop in usage
for (int a=1; a<=10; a++) {
if(a==10) {
//loop is breaked
break.
}
System.out.println(a).
}
}
}
Output:
1
2
3
4
5
6
7
8
9
Usage of Break Statement in Inner Loops of the java program
Inner loop is breaked or stops execution only if the break statement or keywprd is used in that given loop.
Example:
BreakEx2.java
public class BreakEx2 {
public static void main (String [] args) {
//outer loop
for (int m=1;m<=3;m++){
//using inner loop
for (int n=1;n<=4;n++){
if(m==2&&n==2) {
//inside the inner loop break statements is used.
break.
}
System.out.println(m+" "+n);
}
}
}
}
Output:
11
1 2
1 3
14
2 1
3 1
3 2
3 4
4 4
Java Break Statement with Labeled for Loop
The break statement is used with a label for terminating the label created program. The feature is developed since JDK 1.5. So, we can terminate any loop in Java now either it is outer or inner loop.
Example:
BreakEx3.java
//to implement the use of continue statementby implementing the break
//with label inside an inner loop to break outer loop
public class BreakEx3 {
public static void main (String [] s) {
cc:
for (int m=1;m<=3;m++) {
dd:
for (int n=1; n<=3;n++) {
if(m==2&&n==2) {
//using break statement with label
break cc.
}
System.out.println(m+" "+n);
}
}
}
}
Output:
1 1
1 2
1 3
2 1
Java Break Statement in while loop
Example:
BreakWhileEx.java
//Java Program to illustrate the use of break statement
//uesd in while loop.
public class BreakWhileEx {
public static void main (String [] s) {
//while loop
int p=1;
while(p<=20) {
if(p==5) {
//using break statement
p++;
break;//it will break the loop
}
System.out.println(p);
p++;
}
}
}
Output:
5
6
7
8
Do-while Loop in Java Break Statements
Example:
BreakDoWhileEx.java
//to develop or illustrate the use of break statement through java program
// Java do-while loop inside loop
public class BreakDoWhileEx {
public static void main (String [] s) {
//variable declaration
int q=5;
//do-while loop
do {
if(q==10) {
//using break statement
q++;
break;//it will break the loop
}
System.out.println(q);
q++;
} while(q<=20);
}
}
Output:
5
6
7
8
9
10