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

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...
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 ...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Resignation Letter and Relieving Letter Resignation Letter Sample 1 Sender’s Name, Sender’s Address, Mobile Number, MailDate:ToThe Human Resource Department Company Name Company Addre...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...

Leave a Reply

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