{"id":356,"date":"2015-12-11T12:25:56","date_gmt":"2015-12-11T12:25:56","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=356"},"modified":"2015-12-11T12:25:56","modified_gmt":"2015-12-11T12:25:56","slug":"switch-in-java","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/switch-in-java","title":{"rendered":"Switch In JAVA With Example &#8211; Complete Tutorial"},"content":{"rendered":"<p>A switch statement is another useful way to control the flow of program which is mostly used in those cases where we need our program to act on a single variable out of several options available. The switch statement first match the case of variable and then act upon the variable if matched.<\/p>\n<hr \/>\n<h4><strong>Switch Syntax:<\/strong><\/h4>\n<pre>switch (variable expression) {\r\ncasevalue1:\r\ncode_or_statements_to_be_executed_if _variable == value1\r\nbreak;\r\ncasevalue2: \r\ncode_or_statements_to_be_executed_if _variable== value2\r\nbreak;\r\ncase value3:        \r\ncode_or_statements_to_be_executed_if _variable_ ==  value3\r\nbreak;\r\n  . . .\r\ndefault:\r\ncode_or_statements_to_be_executed_if _variable_does_not_match_any_of_the_above_case\r\nbreak;\r\n}<\/pre>\n<p><strong>switch:<\/strong> In the above syntax switch keyword has variable inside parenthesis which is tested for equality with the cases.<\/p>\n<p><strong>case:<\/strong> The case keyword has value of same data type as of switch variable which is tested with the equality of value of switch variable. The case whose value matched is executed.<\/p>\n<p><strong>default:<\/strong> If none of the case value is matched with the value of switch variable then code inside default is executed.<\/p>\n<p><strong>break:<\/strong> The break is used inside each case so as to break out of switch statement.<\/p>\n<hr \/>\n<h4><strong>Switch Example In JAVA:<\/strong><\/h4>\n<p>Lets create a program to choose a day of week in JAVA using switch.<\/p>\n<pre>\r\n\r\npublic class SwitchExample {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tint day = 5;\r\n\r\n\t\tswitch (day){\r\n\t\tcase 1:\r\n\t\tSystem.out.println(\"It is Monday\");\r\n\t\tbreak;\r\n\t\tcase 2:\r\n\t\tSystem.out.println(\"It is Tuesday\");\r\n\t\tbreak;\r\n\t\tcase 3:\r\n\t\tSystem.out.println(\"It is Wednesday\");\r\n\t\tbreak;\r\n\t\tcase 4:\r\n\t\tSystem.out.println(\"It is Thursday\");\r\n\t\tbreak;\r\n\t\tcase 5:\r\n\t\tSystem.out.println(\"It is Friday\");\r\n\t\tbreak;\r\n\t\tcase 6:\r\n\t\tSystem.out.println(\"It is Saturday\");\r\n\t\tbreak;\r\n\t\tcase 7:\r\n\t\tSystem.out.println(\"It is Sunday\");\r\n\t\tbreak;\r\n\t\tdefault:\r\n\t\tSystem.out.println(\"Entered wrong input\");\r\n\t\tbreak;\r\n\t\t}\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>Output is:<\/p>\n<pre>It is Friday<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A switch statement is another useful way to control the flow of program which is mostly used in those cases where we need our program to act on a single variable out of several options available. The switch statement first match the case of variable and then act upon the variable if matched. Switch Syntax: &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/switch-in-java\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Switch In JAVA With Example &#8211; Complete Tutorial<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-356","page","type-page","status-publish","hentry"],"psp_head":"<title>Switch In JAVA With Example - Complete Tutorial \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Complete tutorial on Switch in JAVA with examples. A switch statement is another useful way to control the flow of program which is mostly used in those cases where we need our program to act on a single variable out of several options available.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/switch-in-java\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=356"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/356\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}