{"id":890,"date":"2024-11-07T10:57:22","date_gmt":"2024-11-07T02:57:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/890\/"},"modified":"2024-11-07T10:57:22","modified_gmt":"2024-11-07T02:57:22","slug":"c-programming-a-short-and-simple-guide-to-break-continue-and-switch","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/890\/","title":{"rendered":"C Programming: A Short and Simple Guide To break, continue, and switch"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1014\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173044692443851.jpg\" width=\"720\" height=\"288\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173044692443851.jpg 720w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173044692443851-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173044692443851-670x268.jpg 670w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" title=\"C Programming: A Short and Simple Guide To break, continue, and switch\u63d2\u56fe\" alt=\"C Programming: A Short and Simple Guide To break, continue, and switch\u63d2\u56fe\" \/><\/p>\n<p>\u8fd9\u7bc7\u5feb\u901f\u800c\u7b80\u5355\u7684\u6587\u7ae0\u6df1\u5165\u7814\u7a76\u4e86 c \u8bed\u8a00\u4e2d\u66f4\u9ad8\u7ea7\u7684\u63a7\u5236\u6d41\u673a\u5236\uff0c\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u7f16\u5199\u66f4\u9ad8\u6548\u548c\u53ef\u8bfb\u4ee3\u7801\u7684\u5de5\u5177\u3002<\/p>\n<h3> <strong>\u4f11\u606f<\/strong>\u5e76<strong>\u7ee7\u7eed<\/strong> <\/h3>\n<p>\u8fd9\u4e9b\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u64cd\u7eb5\u5faa\u73af\u6267\u884c\u3002<\/p>\n<ul>\n<li> <strong>break<\/strong>\uff1a\u5b8c\u5168\u7ec8\u6b62\u5faa\u73af\u3002 <\/li>\n<\/ul>\n<pre>for (int i = 0; i &lt; 10; i++) {\n  if (i == 5) {\n    break; \n  }\n  printf(\"%d \", i);\n}\n\/\/ output: 0 1 2 3 4\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u7ee7\u7eed<\/strong>\uff1a\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002 <\/li>\n<\/ul>\n<pre>for (int i = 0; i &lt; 5; i++) {\n  if (i == 2) {\n    continue; \n  }\n  printf(\"%d \", i);\n}\n\/\/ output: 0 1 3 4\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>switch<\/strong>\uff1a\u5904\u7406\u5355\u4e2a\u53d8\u91cf\u65f6\u591a\u4e2a if-else \u8bed\u53e5\u7684\u66f4\u6e05\u6670\u7684\u66ff\u4ee3\u65b9\u6848\u3002 <\/li>\n<\/ul>\n<pre>int day = 3;\nswitch (day) {\n  case 1:\n    printf(\"monday\n\");\n    break;\n  case 2:\n    printf(\"tuesday\n\");\n    break;\n  case 3:\n    printf(\"wednesday\n\");\n    break;\n  default:\n    printf(\"other day\n\");\n}\n\/\/ output: wednesday\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>break<\/strong> \u8bed\u53e5\u5728 switch \u5757\u4e2d\u5bf9\u4e8e\u9632\u6b62\u5931\u8d25\u884c\u4e3a\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p><strong>\u6761\u4ef6\u8fd0\u7b97\u7b26 (?:)<\/strong><br \/> \u8868\u8fbe\u7b80\u5355\u6761\u4ef6\u903b\u8f91\u7684\u7b80\u6d01\u65b9\u5f0f\u3002<\/p>\n<pre>int a = 10, b = 20;\nint max = (a &gt; b) ? a : b; \/\/ max will be 20\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u76f8\u5f53\u4e8e\uff1a<\/p>\n<pre>int a = 10, b = 20;\nint max;\nif (a &gt; b) {\n  max = a;\n} else {\n  max = b;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6761\u4ef6\u8fd0\u7b97\u7b26 (?:)<\/strong> \u5982\u679c\u4f7f\u7528\u5f97\u5f53\uff0c\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p>c\u7a0b\u5e8f\u5458\u901a\u8fc7\u638c\u63e1\u63a7\u5236\u6d41\u673a\u5236\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u6709\u7ec4\u7ec7\u3001\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u8fd9\u4e9b\u7ed3\u6784\u5141\u8bb8\u7075\u6d3b\u7684\u7a0b\u5e8f\u6267\u884c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fC Programming: A Short and Simple Guide To break, continue, and switch\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u5feb\u901f\u800c\u7b80\u5355\u7684\u6587\u7ae0\u6df1\u5165\u7814\u7a76\u4e86 c \u8bed\u8a00\u4e2d\u66f4\u9ad8\u7ea7\u7684\u63a7\u5236\u6d41\u673a\u5236\uff0c\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u7f16\u5199\u66f4\u9ad8\u6548\u548c\u53ef\u8bfb\u4ee3\u7801\u7684\u5de5\u5177\u3002 \u4f11\u606f\u5e76\u7ee7\u7eed \u8fd9\u4e9b\u5173\u952e\u5b57\u5141\u8bb8\u6211\u4eec\u64cd\u7eb5\u5faa\u73af\u6267\u884c\u3002 break\uff1a\u5b8c\u5168\u7ec8\u6b62\u5faa\u73af\u3002 for (int i = 0; i &lt; 10; i++) { if (i == 5) { break; } printf(&#8220;%d &#8220;, i); } \/\/ output: 0 1 2 3 4 \u767b\u5f55\u540e\u590d\u5236 \u7ee7\u7eed\uff1a\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002 for (int i = 0; i &lt; 5; i++) { if (i == 2) { continue; } printf(&#8220;%d &#8220;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=890"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/890\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}