Latest :

Java Break – Tutorial For Beginners

Java break statement complete tutorial for beginners with examples.  You can check out the more tutorials and beginner programs here. Java Break statement complete guide with examples. Check the table of contents here. If you want any custom program for your project or hire as a freelancer you can contact us here.

Table Of Contents :

Q : What is Java Break? Definition Of Statement?

  • It is a statement used to come out of a switch or a looping construct. The looping construct can be a while loop, for loop or a do-while loop.

Java Break : Tutorial For Beginners With Examples

1) Java Switch Break 

[wp_ad_camp_3]

  • Generally Java switch break is used at end of each case block in a switch construct. With the java break ‘ control ‘ comes out of switch construct. If it is omitted, than control automatically enters into the next case block.

Pattern:

 

In the above example ( Java Switch Break ), if the value of expression is 1, than control enters the case 1 block and executes the statements in it.

  • As the statements are completed, the break is encountered. With that, the control comes out of the switch construct and executes the statement immediately following by the switch construct.

In case if it is omitted, than control automatically enters into case 2 block and executes the statements in it.

In switch construct, it is used unconditionally at end of each case block. But as per our requirements we can use it conditionally also at middle of a case block. Below is the sample example program for Java switch break.

Example: 

 

From the above “Java switch ” example, if x==2 is true, then only “mango” is printed. Otherwise both “mango” and “grape” will be printed. In this example, if the Java Switch is not at the end of the case block.

[wp_ad_camp_4]

  • Here it is used conditionally. The Switch break after printing “grape” is unconditional usage.

By default, the break is applied on the current construct.

It means, if we have a nested switching construct and a it is applied in inner switch, then the control comes out of the inner switch but not from outer switch – Learn more about java switch case statement with examples.

Example : 2:

 

Output:

From the above example, case 1 is matched with the outer switch expression and “apple” is printed.

[wp_ad_camp_2]

Then it enters into inner switch and case 21 is matched. So “mango” is printed. Then a ‘break’ is found. As this break is in the inner switch, control comes of the inner switch and executes the statement immediately following the inner switch. Hence, “orange” is printed. After printing “orange” there is another ‘break’. This break will take the control out of the outer switch.

2. Break With A Label In Switch ( Java Break Label )

 

  • If we want to come out from an outer switch, directly from an inner switch then we can use Java break with a label where the label refers outer switch (This kind of facility is not available in languages like C and C++).

Example:

Outer:

 

From the above Java Label, example, the cases 859 and 684 are matched with the corresponding switch expressions and accordingly “devaki” and “himani” will be printed.

Then there is a statement with Outer as its “label”. As Outer refers to the ” outer switch “ , the statement ‘break Outer’ will take the control out of the outer switch.

 

3.Break Loop

  • Break loop can be used to come out of a looping construct from a middle of the body. Generally a loop is terminated when the condition in the header part results in false.

But sometimes we may need to come out of the loop when a particular situation arise in middle of the body. So, generally it is used conditionally in loops (in switch, generally it is used unconditionally at end of a case block).

Example 1: 

When condition 1 is false, program will drop out of the loop, when condition 2 is true we will come out of loop. The bre;ak can be used to come out of any loop i.e. while or do-while or for loop.

Example 2: 

 

  • From the above example, the loop is repeatedly executed as long as (f<=n) results in true. When it becomes false the loop terminates in general.

Additionally, the Break loop will be terminated in case (n%f==0) results in true. Whatever takes place first, with that reason the loop will stop.

Example 3:

From the above example, the do-while is executed as long as (x<=y). in case (a==c) is true before (x<=y) becomes false, then ” a “is assigned with ” d “ and loop ends.

 

4. Break In An Inner Loop

  • Break is always applied on the current loop. When a loop is inside another loop and it is executed in the inner loop, then the control comes out of the inner loop only, but not from outer loop. If we want to come out of outer loop also we have to write another java break statement, with appropriate condition, in the outer loop.

Example:

 

  • From the above example, when
    (a[i][j]!=b[i][j])
    is true. Than corresponding funtion will terminate in inner loop (but not the outer loop).
  • So after coming out of the inner loop, we have written another br;ak, conditionally, to come out of the outer loop. If the condition (j<cols) is false then the outer loop continues.
5. Break With A Label In Loops

 

  • From the, above example, the statement takes out of the inner loop when (a[i][j]!=b[i][j]) is true. If we want to come out of the outer loop directly from that condition we can use bre;ak statement with label where the label represents the outer loop. The name of the label is our choice just like a variable.

Example: 

ROWS:

  • From the above example, the “break ROWS” will take the control out the outer (ROWS) loop. This facility will help us to minimize the code and readability of the program will also be bettered.

This is an alternative way to “goto” concept found in other languages.

The “goto” mechanism is strongly discouraged to use as it makes the readability and flow of control of the program more difficult.

 

6. Break Should Not Be Used In Absence Of Switch And Loop

 

A bre;ak must be used inside a switch or a loop only. Without these constructs, we should not use break. Suppose we use it in an, if construct where there if is not in switch/loop then a compiler error will be raised.

Example:      

 

The above program will raise an error because if  there is no loop or switch but break is used. Similarly the following program also will raise an compilation error.

Example:       

 

Learn More Tutorials:

x

Check Also

Java If Else – Tutorial With Examples | Learn Java

If else Java – statement complete tutorial. Here we cover in-depth information with examples on ...