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

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...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the t...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
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...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
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 *