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 Java Programming Language JAVA TUTORIALS Introduction to Java Programming Language What is JAVA Java is programming language that produces software for multiple platforms, w...
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 ...
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...
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...
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 ...
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 *