{"id":322,"date":"2023-10-14T08:35:54","date_gmt":"2023-10-14T00:35:54","guid":{"rendered":"https:\/\/wp.minicoda.com\/?p=322"},"modified":"2025-03-17T13:23:43","modified_gmt":"2025-03-17T05:23:43","slug":"switch-case%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/wp.minicoda.com\/index.php\/switch-case%e8%af%ad%e5%8f%a5\/","title":{"rendered":"switch\/case\u8bed\u53e5"},"content":{"rendered":"\n<p>  \u591a\u8def\u5206\u652f\u9009\u62e9switch\/case\u662fif\/else if\/else\u7684\u597d\u770b\u7248\u672c\uff0cswitch\u4e2ddefault\u8bed\u53e5\u5bf9\u5e94if\u7684else\u8bed\u53e5\u3002\u6bcf\u4e2acase\u5bf9\u5e94\u6c47\u7f16\u4ee3\u7801\u7684label, \u7f16\u8bd1\u5668\u63d2\u5165\u591a\u6761jmp\u8bed\u53e5\u5b9e\u73b0\u4e0d\u540c\u5206\u652f\u8df3\u8f6c.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6761\u4ef6\u7c7b\u578b<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\/C++\u6761\u4ef6\u7c7b\u578b\u5fc5\u987b\u662f\u6574\u578b\u3001\u5b57\u7b26\u6216\u679a\u4e3e\uff0c\u4e0d\u80fd\u662f\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>C#\u9664\u4e86C\/C++\u652f\u6301\u7684\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u652f\u6301\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0cJava 7 switch\u6761\u4ef6\u52a0\u5165\u4e86\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/li>\n\n\n\n<li>Python 3.10\u52a0\u5165\u4e86match case\u8bed\u53e5\u652f\u6301\u591a\u6761\u4ef6\u9009\u62e9\u3002<\/li>\n\n\n\n<li>Ada\u7528case\/when\u7ed3\u6784\u3002<\/li>\n\n\n\n<li>Shell\u7684case\u8bed\u53e5\u4f7f\u7528\u901a\u914d\u7b26\u6d4b\u8bd5\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f62\u5982*.txt\u5339\u914d\u6240\u6709\u4ee5.txt\u7ed3\u5c3e\u7684\u6587\u672c\uff0c\u8fd9\u6bd4\u7f16\u8bd1\u578b\u8bed\u8a00\u66f4\u81ea\u7531\u3002Pattern\u8fd8\u53ef\u7528|\u7ec4\u5408\u505a\u591a\u91cd\u5339\u914d\u3002<\/li>\n\n\n\n<li>PHP\u4f5c\u4e3a\u811a\u672c\u8bed\u8a00\uff0ccase\u53c2\u6570\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>Kotlin \u7528when\u4ee3\u66ff\u4ee5\u5f80\u7684switch\uff0c\u4f8b\u5982\uff1a<br>&nbsp;when (a) {<br>&nbsp; &nbsp; &nbsp; &nbsp; 1 -&gt; println(\"a is 1\")<br>&nbsp; &nbsp; &nbsp; &nbsp; 2 -&gt; println(\"a is 2\")<br>&nbsp; &nbsp; &nbsp; &nbsp; else -&gt; println(\"a is not 1, 2\")<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; when (a) &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; 1, 2 -&gt; println(\"a is 1 or 2\")<br>&nbsp; &nbsp; &nbsp; &nbsp; else -&gt; println(\"a is not 1, 2\")<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; when {<br>&nbsp; &nbsp; &nbsp; &nbsp; a == 1 -&gt; println(\"a is 1\")<br>&nbsp; &nbsp; &nbsp; &nbsp; a == 2 -&gt; println(\"a is 2\")<br>&nbsp; &nbsp; &nbsp; &nbsp; else -&gt; println(\"a is not 1, 2\")<br>&nbsp; &nbsp; }<\/li>\n\n\n\n<li>Pascal \u91c7\u7528 case xxx of \u5f00\u5934\uff0c\u540e\u9762\u7d27\u8ddf\u4e0d\u540ccase\u4ee5\u53caelse. \u4f8b\u5982\uff1a<br>case ch of<br>  'a' : Writeln(\"a\");<br>  else: Writeln(\"not a\");<br>end;\n<ul class=\"wp-block-list\">\n<li>case\u53c2\u6570\u53ef\u4ee5\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u7528 .. \u6307\u4ee3\u8fde\u7eed\u8303\u56f4\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4ed3\u9889 \u63d0\u4f9bmatch\/case\u6761\u4ef6\u5339\u914d\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">switch\u7ed3\u6784\u8d77\u6e90<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6700\u65e9Algol 68\u5f15\u5165\u4e86\u591a\u91cd\u9009\u62e9\u8bed\u53e5switch\/case, \u4e3a\u540e\u7eed\u5927\u90e8\u5206\u7f16\u7a0b\u8bed\u8a00\u6240\u7528\uff0c\u6bd4\u5982C\/C++\/Java\/C#.<\/li>\n\n\n\n<li>Fortran\u6700\u65e9\u6ca1\u6709\uff0c\u5728Fortran 77\u7248\u672c\u5f15\u5165\u4e86\u7c7b\u4f3c\u7684select\/case\u8bed\u53e5\u3002<\/li>\n\n\n\n<li>Cobol 85\u5f15\u5165\u4e86evaluate\u8bed\u53e5\uff0c\u7c7b\u4f3c\u4e8eswitch\/case.<\/li>\n\n\n\n<li>Ada\u652f\u6301case\u4e00\u6761\u8bed\u53e5\u591a\u6761\u4ef6\u548c\u8303\u56f4\u8bbe\u5b9a\uff0c\u5f62\u5982when 1 | 2 | 10 .. 15\u8868\u793a\u4e3a1\u62162\u621610~15\u7684\u8303\u56f4\u3002<\/li>\n\n\n\n<li>\u6807\u51c6C\u8bed\u8a00\u4e0d\u652f\u6301case\u8bed\u53e5\u8303\u56f4\uff0cGNU C\u6269\u5c55\u4e86\u6b64\u529f\u80fd\uff0c\u793a\u4f8b\uff1acase 10 ... 1000: \u8868\u793a\u572810\u5230100\u8303\u56f4\u5185\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fall Through<\/h2>\n\n\n\n<p>  case\u8d2f\u7a7f\u5728\u67d0\u4e9b\u65f6\u5019\u7279\u522b\u6709\u6548\uff0c\u6bd4\u5982libc %i\u7528%d\u540c\u4e00\u5957\u8f93\u51fa\u903b\u8f91\uff0c\u4ec5\u4ec5\u5c06%i\u7684case\u548c%d\u5730\u65b9\u4e00\u8d77\u5373\u53ef\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\/C++\u6709\u7740\u8fd1\u4e4e\u6c47\u7f16\u7684\u63a7\u5236\u80fd\u529b, \u5bf9fall through\u7684\u652f\u6301\u6700\u81ea\u7531\u3002<\/li>\n\n\n\n<li>Java\u4e5f\u652f\u6301\u8d2f\u7a7f\uff0c\u56e0\u4e0d\u652f\u6301goto\u5bf9\u4e8e\u67d0\u4e9b\u5904\u7406\u903b\u8f91\u4e0d\u90a3\u4e48\u7075\u6d3b\u3002<\/li>\n\n\n\n<li>C#\u66f4\u6709\u610f\u601d\uff0c\u652f\u6301\u8d2f\u7a7f\uff0c\u4f46\u5fc5\u987b\u7528goto\u8bed\u53e5\u663e\u5f0f\u6307\u660e\u8df3\u8f6c\u54ea\u91cc\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u8def\u5206\u652f\u9009\u62e9switch\/case\u662fif\/else if\/else\u7684\u597d\u770b\u7248\u672c\uff0cswitch\u4e2ddefault\u8bed\u53e5\u5bf9\u5e94if\u7684else\u8bed\u53e5\u3002\u6bcf\u4e2acase\u5bf9\u5e94\u6c47\u7f16\u4ee3\u7801\u7684label, \u7f16\u8bd1\u5668\u63d2\u5165\u591a\u6761jmp\u8bed\u53e5\u5b9e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,116,8,100],"tags":[119,118,117,9],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-114","category-116","category-8","category-100","tag-case","tag-switch","tag-117","tag-9"],"views":66,"_links":{"self":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":9,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":3964,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions\/3964"}],"wp:attachment":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}