<p class="p1">In this article, you will learn to use <span style="color: #0000ff;"><strong>switch</strong></span> statement to control the flow of your program’s execution.</p>
<h3><span style="color: #000080;"><strong>Java switch Statement</strong></span></h3>
<p>When we want to execute a particular <span style="color: #008000;"><strong>block of code/statements</strong></span> among many blocks we can use <span style="color: #008000;"><strong>switch</strong></span> statement.</p>
<h4><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<pre><strong><span style="color: #008000;">// switch statement</span> </strong>
switch(expression/variable)
{
 <span style="color: #008000;"><strong> // case statements</strong></span>

 <strong><span style="color: #008000;">// values must be of same type of expression</span></strong>
 case value1 :
 <strong><span style="color: #008000;">// Statements</span></strong>
 break; <strong><span style="color: #008000;">// break is optional</span></strong>

 case value2 :
 <strong><span style="color: #008000;"> // Statements</span></strong>
 break; <strong><span style="color: #008000;">// break is optional</span></strong>

 <strong><span style="color: #008000;">// We can have any number of case statements</span></strong>
<strong><span style="color: #008000;"> // below is default statement, used when none of the cases is true. </span></strong>
<strong><span style="color: #008000;"> // No break is needed in the default case.</span></strong>
 default :
 <strong><span style="color: #008000;"> // Statements</span></strong>
}</pre>
<p class="p1">This is how it works:</p>
<ul class="ul1">
<li class="li1">The switch statement first evaluates its <span style="color: #0000ff;"><strong>expression</strong></span> and compares it with multiple <span style="color: #0000ff;"><strong>cases</strong></span>.</li>
<li class="li1">If there is a match, execute all statements of the matching case label.</li>
<li>
<p class="p1">For example, the <span style="color: #008000;"><strong>variable/expression</strong></span> is equal to value2. In this case, all statements of case label <span style="color: #008000;"><strong>value2</strong></span> gets executed.</p>
</li>
<li>
<p class="p1">The <span style="color: #0000ff;"><strong>break</strong></span> statement is optional. If used, move the control out of the switch block. If omitted, all statements after the matching case label are executed in sequence until the end of switch statement.</p>
</li>
<li class="li1">The <strong><span style="color: #0000ff;">default</span></strong> statement is optional. If no match is found, then the default statement is executed, and then the control goes out of the switch block.</li>
</ul>
<h4><strong><span style="color: #0000ff;">Flowchart:</span></strong></h4>
<p><img class=" wp-image-1780 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/switch-exp-img.png" alt="switch-exp-img" width="627" height="572" /></p>
<h4><strong><span style="color: #0000ff;">Example</span></strong></h4>
<pre>public class Example {

 public static void main(String[] args) {

 int day = 5;

 <span style="color: #008000;"><strong>// switch statement with int data type</strong></span>
 switch (day) {
 case 1:
 System.out.println("Monday");
 break;
 case 2:
 System.out.println("Tuesday");
 break;
 case 3:
 System.out.println("Wednesday");
 break;
 case 4:
 System.out.println("Thursday");
 break;
 case 5:
 System.out.println("Friday");
 break;
 case 6:
 System.out.println("Saturday");
 break;
 case 7:
 System.out.println("Sunday");
 break;
 default:
 System.out.println("Invalid day");
 }
 System.out.println("Outside switch block");

 }
}

</pre>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>Friday
Outside switch block</pre>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p>In the above example, <strong>when day = 5:</strong></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul>
<li>switch will execute the case whose case-label is 5 and print <span style="color: #008000;"><strong>&#8220;Friday&#8221;</strong></span>.</li>
<li>When it encounters <strong><span style="color: #008000;">break</span></strong> statement, the control will skip all the rest cases and move out of the switch block.</li>
<li>print <span style="color: #008000;"><strong>&#8220;Outside switch block&#8221;</strong></span>.</li>
</ul>
<p><strong>when day = 8:</strong></p>
<ul>
<li>switch found no match case for day variable so it will execute the <strong><span style="color: #0000ff;">default</span></strong> case and print <span style="color: #008000;"><strong>&#8220;Invalid day&#8221;</strong></span>.</li>
<li>Now the control will move out of the switch statement and print <span style="color: #008000;"><strong>&#8220;Outside switch block&#8221;</strong></span>.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Omitting the break statement</strong></span></h3>
<p class="p1">As break statement is optional. If we omit the <strong><span style="color: #008000;">break</span></strong>, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>public class Example {

 public static void main(String[] args) {

 int day = 2;

 switch (day) {
 case 1:
 System.out.println("Monday");

 case 2:
 System.out.println("Tuesday");

 case 3:
 System.out.println("Wednesday");

 case 4:
 System.out.println("Thursday");

 case 5:
 System.out.println("Friday");

 case 6:
 System.out.println("Saturday");
 break;

 case 7:
 System.out.println("Sunday");

 default:
 System.out.println("Invalid day");
 }
 System.out.println("Outside switch block");

 }
}

</pre>
<h4><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>Tuesday
Wednesday
Thursday
Friday
Saturday
Outside switch block</pre>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p>In the above example, <strong>when day = 5:</strong></p>
<ul>
<li>switch expression gets matched with <span style="color: #0000ff;"><strong>case2</strong></span> and will print <strong><span style="color: #008000;">&#8220;Tuesday&#8221;</span></strong>.</li>
<li>switch found no <strong><span style="color: #008000;">break</span></strong> statement inside <strong><span style="color: #0000ff;">case2</span></strong>. So it goes on executing rest cases one by one.</li>
<li>Inside <strong><span style="color: #0000ff;">case6</span></strong> when control found <strong><span style="color: #008000;">break</span></strong> statement, it stop executing rest of the cases and moves out of the switch block.</li>
<li>print <strong><span style="color: #008000;">&#8220;Outside switch block&#8221;</span></strong>.</li>
</ul>
<h4></h4>
<h4 class="p2"><span style="color: #000080;"><b>Rules for switch statement :</b></span></h4>
<ul class="ul1">
<li class="li2">Duplicate case label values are not allowed.</li>
<li class="li2">Case labels must end with a colon (<strong><span style="color: #008000;"> :</span></strong> )</li>
<li class="li2">The value for a case must be of the same data type as the variable in the switch.</li>
<li class="li2">The value for a case must be <strong><span style="color: #008000;">Constant</span></strong>. Variables are not allowed.</li>
<li class="li2">The <span style="color: #008000;"><strong>break</strong></span> statement is optional. If omitted, execution will continue on into the next case.</li>
<li class="li2">The <span style="color: #008000;"><strong>default</strong></span> statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.</li>
</ul>


