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

Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...
Dart Libraries | Dart Tutorial Dart Libraries Dart has the following libraries, which are included in all Dart platforms: dart:core This library is automatically imported into ev...
Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
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...
Introduction To C A Brief History Of C   C was invented and first implemented by Dennis Ritchie on a DEC PDP-11 that used the Unix operating system. C is the r...

Leave a Reply

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