Java Break Statement

 

In programming languages the break statement is used to break through a loop or a switch statement. The break statement is used to break the flow of the loop from the current position and the control is transferred out of the loop.

 

Break statement in switch statement:

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

Syntax:

The following is the syntax of the break statement in Java:

break;

 

Flowchart of break statement in Java:

The following is the flowchart of break statement when it is used within a loop to exit from the loop or to break the flow of the loop moving the control out of loop:

flowchart-of-break-statement-in-java


Java break statement with for loop:

Consider the following example in which we have used the break statement within the “for” loop:

CODE:

public class ExampleBreak {

public static void main (string [] args) {

for (int i = 1; i <= 10; i++) {

if (i == 5) {

break;

}

system. out. println (“Country”);

}

}

}

OUTPUT:

Country

Country

Country

Country

In the above example for loop is used and in the “for” loop we used if condition. When the value of the counter variable i become equal to 5 then the loop is broken and the control is transferred out of the loop. In this way we got our output five times and not ten times.


Java Break statement with inner Loop:

In Java we used the break statement in the inner loop to break the inner loop. Consider the following example in which we have used the break statement in the inner loop of nested loop to break the inner loop according to a condition:

CODE:

public class ExampleLabeledFor {

public static void main (string [] args){

for (int j = 1; j <= 3; j++) {

for (int i = 1; i <= 3; i++) {

if (i < =3) {

break;

}

system. out. println (i + “ ” +j);

}

}

}

}

OUTPUT:

1 1

2 1

In the above example nested loop is used and we used the break statement to break the inner loop, if we labeled the loops then we could break the outer loop also by using its label. If no label is used the inner loop will be broken.


Java break statement with switch:

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.


Example of break statement in switch statement in Java:

Consider the following code in which the switch statement is used to display the name of the fruits according to the number entered for example 1 for Apple, 2 for Mango and so on.

CODE:

public class ExampleSwitch {

public static void main (string [] args) {

{

int n = 3;

Switch (n)

{

case 1:

system. out. println (“Apple”);

break;

case 2:

system. out. println (“Mango”);

break;

case 3:

system. out. println (“Banana”);

break;

case 4:

system. out. println (“Peach”);

break;

case 5:

system. out. println (“Avocado”);

break;

case 6:

system. out. println (“Apricot”);

break;

case 7:

system. out. println (“Melon”);

break;

default:

system. out. println (“out of range”);

}

OUTPUT:

Banana

In the above example an integer type variable is declared named “n”. The variable n is initialized with value 3. The control enters the switch statement, the switch statement takes the value of n and compares it with all the cases when the value of n is matched with any of the case the statements in the body of that case will be executed and then the break statement is also executed. The message is printed and the control moves out of switch statement.


Java switch statement without using break statement:

By Java switch statement is fall through we mean that if the break statement is not used after the statements of each case then all the cases will be executed even if the match is found.

Consider the following example in which we have used the same program as above but did not use the break statement at the end of each case:

CODE:

public class ExampleSwitch {

public static void main (string [] args) {

{

int n = 3;

Switch (n)

{

case 1:

system. out. println (“Apple”);

case 2:

system. out. println (“Mango”);

case 3:

system. out. println (“Banana”);

case 4:

system. out. println (“Peach”);

case 5:

system. out. println (“Avocado”);

case 6:

system. out. println (“Apricot”);

case 7:

system. out. println (“Melon”);

default:

system. out. println (“Out of range”);

}

OUTPUT:

Banana

Peach

Avocado

Apricot

Melon

Out of range

In the above example the first two cases are not executed because the condition is not true. Then the case three is matched and it is executed. Note that there was no break statement; hence all the other cases will be executed.