Site icon C1CTech

Java switch Statement

<p class&equals;"p1">In this article&comma; you will learn to use <span style&equals;"color&colon; &num;0000ff&semi;"><strong>switch<&sol;strong><&sol;span> statement to control the flow of your program’s execution&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Java switch Statement<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>When we want to execute a particular <span style&equals;"color&colon; &num;008000&semi;"><strong>block of code&sol;statements<&sol;strong><&sol;span> among many blocks we can use <span style&equals;"color&colon; &num;008000&semi;"><strong>switch<&sol;strong><&sol;span> statement&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; switch statement<&sol;span> <&sol;strong>&NewLine;switch&lpar;expression&sol;variable&rpar;&NewLine;&lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; case statements<&sol;strong><&sol;span>&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; values must be of same type of expression<&sol;span><&sol;strong>&NewLine; case value1 &colon;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Statements<&sol;span><&sol;strong>&NewLine; break&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; break is optional<&sol;span><&sol;strong>&NewLine;&NewLine; case value2 &colon;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Statements<&sol;span><&sol;strong>&NewLine; break&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; break is optional<&sol;span><&sol;strong>&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; We can have any number of case statements<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; below is default statement&comma; used when none of the cases is true&period; <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; No break is needed in the default case&period;<&sol;span><&sol;strong>&NewLine; default &colon;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Statements<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">This is how it works&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">The switch statement first evaluates its <span style&equals;"color&colon; &num;0000ff&semi;"><strong>expression<&sol;strong><&sol;span> and compares it with multiple <span style&equals;"color&colon; &num;0000ff&semi;"><strong>cases<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li class&equals;"li1">If there is a match&comma; execute all statements of the matching case label&period;<&sol;li>&NewLine;<li>&NewLine;<p class&equals;"p1">For example&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong>variable&sol;expression<&sol;strong><&sol;span> is equal to value2&period; In this case&comma; all statements of case label <span style&equals;"color&colon; &num;008000&semi;"><strong>value2<&sol;strong><&sol;span> gets executed&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>break<&sol;strong><&sol;span> statement is optional&period; If used&comma; move the control out of the switch block&period; If omitted&comma; all statements after the matching case label are executed in sequence until the end of switch statement&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<li class&equals;"li1">The <strong><span style&equals;"color&colon; &num;0000ff&semi;">default<&sol;span><&sol;strong> statement is optional&period; If no match is found&comma; then the default statement is executed&comma; and then the control goes out of the switch block&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Flowchart&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><img class&equals;" wp-image-1780 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;switch-exp-img&period;png" alt&equals;"switch-exp-img" width&equals;"627" height&equals;"572" &sol;><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>public class Example &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; int day &equals; 5&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; switch statement with int data type<&sol;strong><&sol;span>&NewLine; switch &lpar;day&rpar; &lbrace;&NewLine; case 1&colon;&NewLine; System&period;out&period;println&lpar;"Monday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 2&colon;&NewLine; System&period;out&period;println&lpar;"Tuesday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 3&colon;&NewLine; System&period;out&period;println&lpar;"Wednesday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 4&colon;&NewLine; System&period;out&period;println&lpar;"Thursday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 5&colon;&NewLine; System&period;out&period;println&lpar;"Friday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 6&colon;&NewLine; System&period;out&period;println&lpar;"Saturday"&rpar;&semi;&NewLine; break&semi;&NewLine; case 7&colon;&NewLine; System&period;out&period;println&lpar;"Sunday"&rpar;&semi;&NewLine; break&semi;&NewLine; default&colon;&NewLine; System&period;out&period;println&lpar;"Invalid day"&rpar;&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside switch block"&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>Friday&NewLine;Outside switch block<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>In the above example&comma; <strong>when day &equals; 5&colon;<&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul>&NewLine;<li>switch will execute the case whose case-label is 5 and print <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Friday&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>When it encounters <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span><&sol;strong> statement&comma; the control will skip all the rest cases and move out of the switch block&period;<&sol;li>&NewLine;<li>print <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Outside switch block&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>when day &equals; 8&colon;<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>switch found no match case for day variable so it will execute the <strong><span style&equals;"color&colon; &num;0000ff&semi;">default<&sol;span><&sol;strong> case and print <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Invalid day&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>Now the control will move out of the switch statement and print <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8220&semi;Outside switch block&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Omitting the break statement<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">As break statement is optional&period; If we omit the <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span><&sol;strong>&comma; execution will continue on into the next case&period; It is sometimes desirable to have multiple cases without break statements between them&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>public class Example &lbrace;&NewLine;&NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&NewLine; int day &equals; 2&semi;&NewLine;&NewLine; switch &lpar;day&rpar; &lbrace;&NewLine; case 1&colon;&NewLine; System&period;out&period;println&lpar;"Monday"&rpar;&semi;&NewLine;&NewLine; case 2&colon;&NewLine; System&period;out&period;println&lpar;"Tuesday"&rpar;&semi;&NewLine;&NewLine; case 3&colon;&NewLine; System&period;out&period;println&lpar;"Wednesday"&rpar;&semi;&NewLine;&NewLine; case 4&colon;&NewLine; System&period;out&period;println&lpar;"Thursday"&rpar;&semi;&NewLine;&NewLine; case 5&colon;&NewLine; System&period;out&period;println&lpar;"Friday"&rpar;&semi;&NewLine;&NewLine; case 6&colon;&NewLine; System&period;out&period;println&lpar;"Saturday"&rpar;&semi;&NewLine; break&semi;&NewLine;&NewLine; case 7&colon;&NewLine; System&period;out&period;println&lpar;"Sunday"&rpar;&semi;&NewLine;&NewLine; default&colon;&NewLine; System&period;out&period;println&lpar;"Invalid day"&rpar;&semi;&NewLine; &rcub;&NewLine; System&period;out&period;println&lpar;"Outside switch block"&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>Tuesday&NewLine;Wednesday&NewLine;Thursday&NewLine;Friday&NewLine;Saturday&NewLine;Outside switch block<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>In the above example&comma; <strong>when day &equals; 5&colon;<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>switch expression gets matched with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>case2<&sol;strong><&sol;span> and will print <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;Tuesday&&num;8221&semi;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li>switch found no <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span><&sol;strong> statement inside <strong><span style&equals;"color&colon; &num;0000ff&semi;">case2<&sol;span><&sol;strong>&period; So it goes on executing rest cases one by one&period;<&sol;li>&NewLine;<li>Inside <strong><span style&equals;"color&colon; &num;0000ff&semi;">case6<&sol;span><&sol;strong> when control found <strong><span style&equals;"color&colon; &num;008000&semi;">break<&sol;span><&sol;strong> statement&comma; it stop executing rest of the cases and moves out of the switch block&period;<&sol;li>&NewLine;<li>print <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;Outside switch block&&num;8221&semi;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Rules for switch statement &colon;<&sol;b><&sol;span><&sol;h4>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li2">Duplicate case label values are not allowed&period;<&sol;li>&NewLine;<li class&equals;"li2">Case labels must end with a colon &lpar;<strong><span style&equals;"color&colon; &num;008000&semi;"> &colon;<&sol;span><&sol;strong> &rpar;<&sol;li>&NewLine;<li class&equals;"li2">The value for a case must be of the same data type as the variable in the switch&period;<&sol;li>&NewLine;<li class&equals;"li2">The value for a case must be <strong><span style&equals;"color&colon; &num;008000&semi;">Constant<&sol;span><&sol;strong>&period; Variables are not allowed&period;<&sol;li>&NewLine;<li class&equals;"li2">The <span style&equals;"color&colon; &num;008000&semi;"><strong>break<&sol;strong><&sol;span> statement is optional&period; If omitted&comma; execution will continue on into the next case&period;<&sol;li>&NewLine;<li class&equals;"li2">The <span style&equals;"color&colon; &num;008000&semi;"><strong>default<&sol;strong><&sol;span> statement is optional and can appear anywhere inside the switch block&period; In case&comma; if it is not at the end&comma; then a break statement must be kept after the default statement to omit the execution of the next case statement&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version