Break Statement in JAVA

Break Statement in JAVA

The break statement is usually used in following two scenarios:

  • The break statement terminates the loop immediately, and the control of the program moves to the next statement following the loop.
    The important point to note here is that when a break statement is used inside a nested loop, then only the inner loop gets terminated.
    It is almost always used with decision making statements (if…else Statement).
  • It is also used in switch case control. Generally all cases in “switch-case” are followed by a break statement so that whenever the program control jumps to a case, it doesn’t execute subsequent cases. As soon as a break is encountered in switch-case block, the control comes out of the switch-case body.
Syntax:

The syntax of this is break word followed by semicolon i.e.


1
<span class="hljs-keyword">break</span>;
Flow Diagram of Break Statement:
How break statement works?:

Example – Break Statement inside FOR Loop:

1
2
3
4
5
6
7
8
9
10
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BreakExample1</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">10</span>; ++i) {      
         <span class="hljs-keyword">if</span> (i == <span class="hljs-number">5</span>) {
            <span class="hljs-keyword">break</span>;
         }      
         System.out.println(i);
      }  
   }
}
Output:

1
2
3
4
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">4</span>

In the above program, when the value of i becomes 5, expression i == 5 inside the parenthesis of if statement is evaluated to true. Then, The break statement is executed terminates the for loop.

Example – Break Statement inside WHILE Loop:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BreakExample2</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
      <span class="hljs-keyword">int</span> num =<span class="hljs-number">0</span>;
      <span class="hljs-keyword">while</span>(num&lt;=<span class="hljs-number">100</span>)
      {
          System.out.println(<span class="hljs-string">"Value of variable is: "</span>+num);
          <span class="hljs-keyword">if</span> (num==<span class="hljs-number">2</span>)
          {
             <span class="hljs-keyword">break</span>;
          }
          num++;
      }
      System.out.println(<span class="hljs-string">"Out of while-loop"</span>);
  }
}
Output:

1
2
3
4
Value of variable is: <span class="hljs-number">0</span>
Value of variable is: <span class="hljs-number">1</span>
Value of variable is: <span class="hljs-number">2</span>
Out of <span class="hljs-keyword">while</span>-loop

In the above example, we have a while loop running from o to 100 but since we have a break statement that only occurs when the loop value reaches 2, the loop gets terminated and the control gets passed to the next statement in program after the loop body.

In case of nested loops, break terminates the innermost loop.
Example – Break Statement inside Switch-Case:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BreakExample3</span> </span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
    <span class="hljs-keyword">int</span> num=<span class="hljs-number">2</span>;
    <span class="hljs-keyword">switch</span> (num)
    {
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
           System.out.println(<span class="hljs-string">"Case 1 "</span>);
           <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
           System.out.println(<span class="hljs-string">"Case 2 "</span>);
           <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
           System.out.println(<span class="hljs-string">"Case 3 "</span>);
           <span class="hljs-keyword">break</span>;
        <span class="hljs-keyword">default</span>:
           System.out.println(<span class="hljs-string">"Default "</span>);
    }
   }
}
Output:

1
Case <span class="hljs-number">2</span>

In this example, we have break statement after each Case block, this is because if we don’t have it then the subsequent case block would also execute. The output of the same program without break would be Case 2 Case 3 Default.

 

Labeled break Statement:

  • Till now we have discussed the unlabeled form of “break” statement, which terminates the innermost for, while, do…while and switch statement.
  • There is another form of “break” statement i.e. “labeled break”, that can be used to terminate the outer loop.

How labeled break statement works?:

In this, “label” is an identifier.

Example: labeled break Statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LabeledBreak</span></span>{
   <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span></span>{
      first:
      <span class="hljs-keyword">for</span>( <span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">5</span>; i++){
         second:
         <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j = <span class="hljs-number">1</span>; j &lt; <span class="hljs-number">3</span>; j ++ ){
            System.out.println(<span class="hljs-string">"i = "</span> + i + <span class="hljs-string">"; j = "</span> +j);
            <span class="hljs-keyword">if</span> ( i == <span class="hljs-number">2</span>)
            <span class="hljs-keyword">break</span> first;
         }
      }
   }
}
Output:

1
2
3
i = <span class="hljs-number">1</span>; j = <span class="hljs-number">1</span>
i = <span class="hljs-number">1</span>; j = <span class="hljs-number">2</span>
i = <span class="hljs-number">2</span>; j = <span class="hljs-number">1</span>

Related Post

Navigation Tab Bar in IOS | Swift tutorial Navigation Tab Bar in IOS | Swift tutorial Different Customizing techniques for Navigation bar with storyboard and code 1) Custom NavigationTabBar b...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...
Introduction to Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...
Dart Introduction | Dart programming language Dart Introduction Dart is a scalable language that you can use to write simple scripts or full featured apps. Whether you’re creating a mobile app, w...
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...
New Tools for 64-bit Support in Xcode 9.3   New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure fu...

Leave a Reply

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.