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

Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Dart Data type | Dart Tutorial Dart Data type Variables can store data of different types, and different data types can do different things. Dart supports the following data typ...
Create Firebase single Page Website Create firebase single page Website tutorial In this tutorial we are study how to create single page website by the use of firebase hosting its very ...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Data Types in Java Data Types in Java Data types represent the different values to be stored in the variable, for example if a variable has int data type, it ...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...

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.