{"id":1162,"date":"2022-02-24T19:16:00","date_gmt":"2022-02-24T19:16:00","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1162"},"modified":"2022-02-24T19:16:00","modified_gmt":"2022-02-24T19:16:00","slug":"switch-statement","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/switch-statement\/","title":{"rendered":"Switch Statement"},"content":{"rendered":"\n<p>In this tutorial, you will learn to use the switch statement in Java to control the flow of your program\u2019s execution with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>switch<\/code>&nbsp;statement allows us to execute a block of code among many alternatives.<\/p>\n\n\n\n<p>The syntax of the&nbsp;<code>switch<\/code>&nbsp;statement in Java is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch (expression) {\n\n  case value1:\n    \/\/ code\n    break;\n  \n  case value2:\n    \/\/ code\n    break;\n  \n  ...\n  ...\n  \n  default:\n    \/\/ default statements\n  }<\/code><\/pre>\n\n\n\n<p><strong>How does the switch-case statement work?<\/strong><\/p>\n\n\n\n<p>The&nbsp;<var>expression<\/var>&nbsp;is evaluated once and compared with the values of each case.<\/p>\n\n\n\n<ul><li>If&nbsp;<var>expression<\/var>&nbsp;matches with&nbsp;<var>value1<\/var>, the code of&nbsp;<code>case value1<\/code>&nbsp;are executed. Similarly, the code of&nbsp;<code>case value2<\/code>&nbsp;is executed if&nbsp;<var>expression<\/var>&nbsp;matches with&nbsp;<var>value2<\/var>.<\/li><li>If there is no match, the code of the&nbsp;<strong>default case<\/strong>&nbsp;is executed.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: The working of the switch-case statement is similar to the\u00a0Java if&#8230;else&#8230;if ladder. However, the syntax of the\u00a0<code>switch<\/code>\u00a0statement is cleaner and much easier to read and write.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example: Java switch Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java Program to check the size\n\/\/ using the switch...case statement\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    int number = 44;\n    String size;\n\n    \/\/ switch statement to check size\n    switch (number) {\n\n      case 29:\n        size = \"Small\";\n        break;\n\n      case 42:\n        size = \"Medium\";\n        break;\n\n      \/\/ match the value of week\n      case 44:\n        size = \"Large\";\n        break;\n\n      case 48:\n        size = \"Extra Large\";\n        break;\n      \n      default:\n        size = \"Unknown\";\n        break;\n\n    }\n    System.out.println(\"Size: \" + size);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Size: Large<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the switch statement to find the size. Here, we have a variable&nbsp;<var>number<\/var>. The variable is compared with the value of each case statement.<\/p>\n\n\n\n<p>Since the value matches with&nbsp;<strong>44<\/strong>, the code of&nbsp;<code>case 44<\/code>&nbsp;is executed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size = \"Large\";\nbreak;<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<var>size<\/var>&nbsp;variable is assigned with the value&nbsp;<code>Large<\/code>.<\/p>\n\n\n\n<p><strong>Recommended Reading<\/strong>:\u00a0Create a Simple Calculator Using the Java switch Statement<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"flowchart\">Flowchart of switch Statement<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/cdn\/farfuture\/ETPdbL9iEV_jJsMQVbbb4KZQTyJhPk_sfoiu7dbZAtk\/mtime:1602652645\/sites\/tutorial2program\/files\/java-switch-case-implementation.png\" alt=\"Flowchart of the Java switch statement\" title=\"Flow chart of the Java switch statement\"\/><figcaption>Flow chart of the Java switch statement<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"break-in-switch\">break statement in Java switch&#8230;case<\/h2>\n\n\n\n<p>Notice that we have been using&nbsp;<code>break<\/code>&nbsp;in each case block.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> ...\ncase 29:\n  size = \"Small\";\n  break;\n...<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>break<\/code>&nbsp;statement is used to terminate the&nbsp;<strong>switch-case<\/strong>&nbsp;statement. If&nbsp;<code>break<\/code>&nbsp;is not used, all the cases after the matching case are also executed. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    int expression = 2;\n\n    \/\/ switch statement to check size\n    switch (expression) {\n      case 1:\n        System.out.println(\"Case 1\");\n\n        \/\/ matching case\n      case 2:\n        System.out.println(\"Case 2\");\n\n      case 3:\n        System.out.println(\"Case 3\");\n\n      default:\n        System.out.println(\"Default case\");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Case 2\nCase 3      \nDefault case<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example,&nbsp;<var>expression<\/var>&nbsp;matches with&nbsp;<code>case 2<\/code>. Here, we haven&#8217;t used the break statement after each case.<\/p>\n\n\n\n<p>Hence, all the cases after&nbsp;<code>case 2<\/code>&nbsp;are also executed.<\/p>\n\n\n\n<p>This is why the\u00a0<code>break<\/code>\u00a0statement is needed to terminate the\u00a0<strong>switch-case<\/strong>\u00a0statement after the matching case. <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default\">default case in Java switch-case<\/h2>\n\n\n\n<p>The switch statement also includes an&nbsp;<strong>optional default case<\/strong>. It is executed when the expression doesn&#8217;t match any of the cases. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n  \n    int expression = 9;\n    \n    switch(expression) {\n        \n      case 2:\n        System.out.println(\"Small Size\");\n        break;\n\n      case 3:\n        System.out.println(\"Large Size\");\n        break;\n            \n      \/\/ default case\n      default:\n        System.out.println(\"Unknown Size\");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Unknown Size<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a&nbsp;<strong>switch-case<\/strong>&nbsp;statement. Here, the value of&nbsp;<var>expression<\/var>&nbsp;doesn&#8217;t match with any of the cases.<\/p>\n\n\n\n<p>Hence, the code inside the&nbsp;<strong>default case<\/strong>&nbsp;is executed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>default:\n  System.out.println(\"Unknown Size);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Note<\/strong>: The Java switch statement only works with:<\/p>\n\n\n\n<ul><li>Primitive data types: byte, short, char, and int<\/li><li>Enumerated types<\/li><li>String Class<\/li><li>Wrapper Classes: Character, Byte, Short, and Integer.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn to use the switch statement in Java to control the flow of your program\u2019s execution with the help of examples. The&nbsp;switch&nbsp;statement allows us to execute a block of code among many alternatives. The syntax of the&nbsp;switch&nbsp;statement in Java is: How does the switch-case statement work? The&nbsp;expression&nbsp;is evaluated once and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1162"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1162"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1162\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}