{"id":4285,"date":"2020-08-08T00:01:39","date_gmt":"2020-08-08T07:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4285"},"modified":"2020-08-08T07:57:30","modified_gmt":"2020-08-08T14:57:30","slug":"the-switch-condition","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4285","title":{"rendered":"The <em>switch<\/em> Condition"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0808-switch_case.png\" alt=\"switch case\" width=\"175\" height=\"93\" class=\"alignnone size-full wp-image-4291\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0808-switch_case.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0808-switch_case-300x159.png 300w\" sizes=\"auto, (max-width: 175px) 100vw, 175px\" \/><br \/>\nA <em>switch-case<\/em> structure performs a complex decision in your code, similar to a cascade of <em>if else-if else<\/em> statements. The structure works like a comparison as a whole, acting upon single values or variables. But its construction need not lack expressions.<br \/>\n<!--more--><br \/>\nIn my books, I describe the <em>switch<\/em> statement like this:<\/p>\n<blockquote><p>Unlike an <em>if<\/em> statement, <em>switch<\/em> eats only a single value.<\/p><\/blockquote>\n<p>And I describe <em>case<\/em> like this:<\/p>\n<blockquote><p>A <em>case<\/em> statement shows a single value . . . followed by a colon.<\/p><\/blockquote>\n<p>It&#8217;s true that <em>switch<\/em> and <em>case<\/em> operate on single values, either a variable, constant, or literal value. But they can also work with expressions. The key is that the expression must result in a single value, as this code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_08-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_08-Lesson-a<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    switch( 4-2 )\r\n    {\r\n        case 4:\r\n            puts(\"The answer is 4\");\r\n            break;\r\n        case 3:\r\n            puts(\"The answer is 3\");\r\n            break;\r\n        case 2:\r\n            puts(\"The answer is 2\");\r\n            break;\r\n        case 1:\r\n            puts(\"The answer is 1\");\r\n            break;\r\n        case 0:\r\n            puts(\"The answer is 0\");\r\n            break;\r\n        default:\r\n            puts(\"I don't know!\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 5, the <em>switch<\/em> keyword&#8217;s parentheses feature an expression, <code>( 4-3 )<\/code>. It&#8217;s not a comparison; the comparison happens between the result of the <em>switch<\/em> expression and what follows the <em>case<\/em> keywords in the structure. The results of the expression is 2 and the <code>case 2:<\/code> statement&#8217;s (starting at Line 13) are executed.<\/p>\n<p>As with <em>switch<\/em>, the <em>case<\/em> keywords can also sport an expression, providing it returns a single value:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_08-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_08-Lesson-b<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    switch( 4-2 )\r\n    {\r\n        case (4-0):\r\n            puts(\"The answer is 4\");\r\n            break;\r\n        case (4-1):\r\n            puts(\"The answer is 3\");\r\n            break;\r\n        case (4-2):\r\n            puts(\"The answer is 2\");\r\n            break;\r\n        case (4-3):\r\n            puts(\"The answer is 1\");\r\n            break;\r\n        case (4-4):\r\n            puts(\"The answer is 0\");\r\n            break;\r\n        default:\r\n            puts(\"I don't know!\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Of course, this example is pedantic; obviously <code>4-2<\/code> matches <code>4-2<\/code>. Still, it&#8217;s the result that&#8217;s compared, not the literal expression.<\/p>\n<p>What happens when you use a comparison in a <em>switch<\/em> statement? Oo! Let&#8217;s find out:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_08-Lesson-c.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_08-Lesson-c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b;\r\n\r\n    a = 10;\r\n    b = -10;\r\n\r\n    switch( a &gt; b  )\r\n    {\r\n        case 1:\r\n            printf(\"%d is greater than %d\\n\",a,b);\r\n            break;\r\n        case 0:\r\n            printf(\"%d is not greater than %d\\n\",a,b);\r\n            break;\r\n        default:\r\n            puts(\"I'm not sure what's going on\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code compiles with a warning. The <em>switch<\/em> statement&#8217;s expression at Line 10 is a &#8220;boolean.&#8221; Still, this warning doesn&#8217;t stop the program from being built. Here&#8217;s the output:<\/p>\n<p><code>10 is greater than -10<\/code><\/p>\n<p>You can suppress the warning by casting the <em>switch<\/em> statement like this:<\/p>\n<p><code>switch( (int)(a > b)  )<\/code><\/p>\n<p>Still it&#8217;s easier to use an <em>if<\/em> statement instead. After all, it&#8217;s the right tool for the job.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>switch<\/em> statement need not contain a single value or variable. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4285\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4285","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4285"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4285\/revisions"}],"predecessor-version":[{"id":4303,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4285\/revisions\/4303"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}