Switch Case Statement

Switch Case Statement

  • The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Switch case statement is used when we have number of options (or choices) and we may need to perform a different task for each choice.
Syntax

1
2
3
4
5
6
7
8
9
10
11
12
<span class="hljs-keyword">switch</span> (variable or an integer expression)
{
     <span class="hljs-keyword">case</span> constant:
     <span class="hljs-comment">//Java code</span>
     ;
     <span class="hljs-keyword">case</span> constant:
     <span class="hljs-comment">//Java code</span>
     ;
     <span class="hljs-keyword">default</span>:
     <span class="hljs-comment">//Java code</span>
     ;
}
Flow Diagram For Switch Cases:
Some Important rules for switch statements :
  • Duplicate case values are not allowed.
  • The value for a case must be the same data type as the variable in the switch.
  • The value for a case must be a constant or a literal.Variables are not allowed.
  • The break statement is used inside the switch to terminate a statement sequence.
  • The break statement is optional. If omitted, execution will continue on into the next case.
  • The default statement is optional, and it must appear at the end of the switch.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SwitchCaseExample1</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-number">2</span>)
     {
        <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
      System.out.println(<span class="hljs-string">"Case1: Value is: "</span>+num);
        <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
      System.out.println(<span class="hljs-string">"Case2: Value is: "</span>+num);
        <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
      System.out.println(<span class="hljs-string">"Case3: Value is: "</span>+num);
        <span class="hljs-keyword">default</span>:
      System.out.println(<span class="hljs-string">"Default: Value is: "</span>+num);
      }
   }
}
Output:

1
Default: Value is: <span class="hljs-number">2</span>

Break Statement used in Switch Case:

Break statements are used when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the execution flow would directly come out of the switch, ignoring rest of the cases

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SwitchCaseExample2</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> i=<span class="hljs-number">2</span>;
      <span class="hljs-keyword">switch</span>(i)
      {
     <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
       System.out.println(<span class="hljs-string">"Case1 "</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">"Case2 "</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">"Case3 "</span>);
       <span class="hljs-keyword">break</span>;
     <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:
           System.out.println(<span class="hljs-string">"Case4 "</span>);
           <span class="hljs-keyword">break</span>;
     <span class="hljs-keyword">default</span>:
       System.out.println(<span class="hljs-string">"Default "</span>);
      }
   }
}
Output:

1
Case2

Related Post

How to install flutter | Flutter tutorial Flutter is the google’s mobile app SDK for develop the high-quality native app for iOS and android. We can develop the app by use of DART and javascri...
Custom View by use of xib in swift Custom View by use of xib in swift We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and...
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...
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...
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 ...
Variables Variables A variable is a named location in memory that is used to hold a value that can be modified by the program. All variable must be declared be...

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.