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

Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
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...
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...
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...
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...
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...

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.