{"id":574,"date":"2015-11-08T15:04:41","date_gmt":"2015-11-08T07:04:41","guid":{"rendered":"http:\/\/yangwang.hk\/?p=574"},"modified":"2015-12-06T17:26:50","modified_gmt":"2015-12-06T09:26:50","slug":"3-8%e3%80%81for%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/yangwang.hk\/?p=574","title":{"rendered":"3.8\u3001for\u5faa\u73af"},"content":{"rendered":"<p>\u5728c\u8bed\u8a00\u4e2d\uff0c\u4e00\u5171\u67093\u79cd\u5faa\u73af\u7ed3\u6784\uff0c\u4e00\u79cd\u662f\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u7684while\uff0c\u5176\u4ed6\u4e24\u79cd\u5219\u662ffor\u548cdo&#8230;while<\/p>\n<p>\u524d\u8005\u662f\u6211\u4eec\u4eca\u5929\u5b66\u4e60\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u76f8\u6bd4\u7b80\u6d01\u7684while\u7ed3\u6784<\/p>\n<p><strong>while(<em>expression<\/em>)<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>for\u7ed3\u6784\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u5b83\u7684\u62ec\u53f7\u5185\u67093\u4e2a\u8868\u8fbe\u5f0f(expression)<\/p>\n<p><strong>for(<em>expression1; expression2; expression3<\/em>)<\/strong><\/p>\n<p><strong>{<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<p>\u7b2c\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff08expression1\uff09<\/p>\n<p>\u4f1a\u5728\u8fdb\u5165for\u5faa\u73af\u524d\u5f00\u59cb\u6267\u884c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u56e0\u6b64\u5b83\u591a\u7528\u4e8e\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7528\u4e0d\u5230\u5b83\uff0c\u4f60\u53ef\u4ee5\u5c06\u5b83\u7a7a\u8d77\u6765\u5982for(; expression2; expression3)\uff0c\u6ce8\u610f\uff0c\u5206\u53f7\u662f\u4e0d\u80fd\u7701\u7565\u7684\uff08\u4e3a\u4ec0\u4e48\u8bf7\u81ea\u5df1\u601d\u8003\uff09<\/p>\n<p>\u4e2d\u95f4\u7684\u8868\u8fbe\u5f0f2\uff0c\u5219\u7c7b\u4f3c\u4e8ewhile\u4e2d\u7684\u8868\u8fbe\u5f0f\uff0c\u5b83\u5c06\u7528\u4e8e\u5224\u65ad\u662f\u5426\u8fdb\u884c\u672c\u6b21\u5faa\u73af<\/p>\n<p>\u5f53\u7ed3\u679c\u4e3a\u771f\u65f6\uff0c\u5b83\u5c06\u8fdb\u884c\u672c\u6b21\u5faa\u73af\uff0c\u5426\u5219\uff0c\u5c06\u8df3\u51fa\u5faa\u73af<\/p>\n<p>\u7b2c\u4e09\u4e2a\u8868\u8fbe\u5f0f\uff0c\u5219\u4f1a\u5728\u6bcf\u6b21\u5faa\u73af\u4f53\u5b8c\u6210\u540e\uff0c\u4e0b\u4e00\u6b21\u5faa\u73af\u5f00\u59cb\u524d\u6267\u884c<\/p>\n<p>\u662f\u4e0d\u662f\u6709\u4e9b\u7cca\u6d82\u4e86\uff1f\u6ca1\u5173\u7cfb\uff0c\u4ee3\u7801\u624d\u662f\u63cf\u8ff0\u7f16\u7a0b\u7684\u6700\u597d\u8bed\u8a00\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\u6765\u81ea3.3\u3001while\u5faa\u73af\u4e00\u8282\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u5b83\u6539\u4e3afor\u7248\u672c<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\nint main()\r\n{\r\n    int i = 0;\r\n    while (i &lt;= 100)\r\n    {\r\n        printf(&quot;%d\\n&quot;, i);\r\n        i = i + 1;\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>for \u7248\u672c<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\nint main()\r\n{\r\n    int i = 0;\r\n    for (; i &lt;= 100; i = i + 1)\r\n    {\r\n        printf(&quot;%d\\n&quot;, i);\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u4e0d\u51fa\u610f\u5916\u7684\u8bdd\uff0c\u8fd9\u4e24\u4e2a\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u679c\u662f\u4e00\u6837\u7684<\/p>\n<p>\u6211\u4eec\u5c06i\u589e\u52a01\u7684\u5de5\u4f5c\u653e\u5230\u4e86expression3\u7684\u4f4d\u7f6e\uff0c\u800c\u7531\u4e8eexpression1 \u7684\u4f4d\u7f6e\u6ca1\u6709\u64cd\u4f5c\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u5b83\u7a7a\u8d77\u6765\u4e86<\/p>\n<p>\u6211\u4eec\u53d1\u73b0\uff0c\u5728\u8fdb\u884c\u5faa\u73af\u524d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cfi\u5e76\u4e14\u5c06\u5b83\u521d\u59cb\u5316\u4e3a0\uff0c\u800cexpression1\u4e5f\u4f1a\u5728\u8fdb\u5165\u5faa\u73af\u4e4b\u524d\u6267\u884c\u4e00\u6b21\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u628a\u7b2c\u516d\u81f3\u4e03\u884c\u6539\u4e3a\uff08\u6211\u4eec\u4f7f\u7528\u4e86++\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\u81ea\u589e\uff09<\/p>\n<p><em><strong>int i;<\/strong><\/em><\/p>\n<p><em><strong>for(i = 0; i &lt;= 100; i++)<\/strong><\/em><\/p>\n<p>\u5728c99\u548c\u4ee5\u540e\u7684\u6807\u51c6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728expression1\u7684\u5730\u65b9\u5b9a\u4e49\u53d8\u91cf\uff08\u5728\u6b64\u4e4b\u524d\u662f\u4e0d\u5141\u8bb8\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fd9\u6837\u5199\uff09<\/p>\n<p><em><strong>for(int i = 0; i &lt;= 100; i++)<\/strong><\/em><\/p>\n<p>\u8fd9\u6837\uff0ci\u5c31\u662f\u4e00\u4e2a\u540d\u526f\u5176\u5b9e\u7684\u201c\u5faa\u73af\u53d8\u91cf\u201d\u4e86\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u5faa\u73af\u662f\u5426\u7ee7\u7eed\uff0c\u800c\u4e14\u5728\u5faa\u73af\u5916\u5b83\u662f\u65e0\u6548\u7684\uff01<\/p>\n<p>\u5728for\u7ed3\u6784\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf <strong>\u4f5c\u7528\u57df<\/strong> \u662f\u5faa\u73af\u5185\u90e8\uff0c\u5728\u5faa\u73af\u5916\u90e8\u7684\u4efb\u4f55\u5730\u65b9\u90fd\u662f\u4e0d\u53ef\u89c1\u800c\u4e14\u65e0\u6548\u7684\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5728\u591a\u4e2a\u5e76\u5217\u7684for\u5faa\u73af\u4e2d\u5b9a\u4e49\u540c\u4e00\u4e2a\u540d\u5b57\u7684\u53d8\u91cf\uff0c\u4f8b\u5982i<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5faa\u73af\u5c55\u5f00\uff0c\u5b83\u662f\u8fd9\u6837\u8fd0\u884c\u7684\uff08\u5e94\u8be5\u6709\u52a9\u4e8e\u4f60\u5f04\u6e05\u5faa\u73af\u7684\u8fc7\u7a0b\uff09<\/p>\n<ol>\n<li>\u8fdb\u5165main\u51fd\u6570<\/li>\n<li>\u5b9a\u4e49\u53d8\u91cfi\uff0c\u5e76\u521d\u59cb\u5316\u4e3a0<\/li>\n<li>\u5224\u65adi &lt;= 100\u662f\u5426\u6210\u7acb\uff08\u663e\u7136\u6210\u7acb\uff09<\/li>\n<li>\u8fdb\u5165\u5faa\u73af\u4f53\uff0c\u6267\u884cprintf();<\/li>\n<li>\u9000\u51fa\u5faa\u73af\u4f53\uff0c\u6267\u884ci++<\/li>\n<li>\u5224\u65adi&lt;=100\u662f\u5426\u6210\u7acb\uff08\u6b64\u5904i\u4e3a1\uff0c\u6210\u7acb\uff09<\/li>\n<li>\u8fdb\u5165\u5faa\u73af\u4f53\uff0c\u6267\u884cprintf();<\/li>\n<li>&#8230;&#8230;.<\/li>\n<li>\u9000\u51fa\u5faa\u73af\u4f53\uff0c\u6267\u884ci++\uff08i=100\uff0c\u6267\u884c\u540ei=101\uff09<\/li>\n<li>\u5224\u65adi&lt;=100\u662f\u5426\u6210\u7acb\uff08\u4e0d\u6210\u7acb\uff09<\/li>\n<li>\u9000\u51fa\u5faa\u73af<\/li>\n<\/ol>\n<p>\u5728for\u7684\u7b2c\u4e09\u4e2a\u8868\u8fbe\u5f0f\u4e2d\u6267\u884c\u5bf9\u4e8e\u90a3\u4e9b\u63a7\u5236\u5faa\u73af\u7684\u53d8\u91cf\u7684\u64cd\u4f5c\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u51b3\u5b9a\uff0c\u56e0\u4e3a\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u5faa\u73af\u4f53\u4e2d\u53ea\u5173\u6ce8\u6211\u4eec\u771f\u6b63\u9700\u8981\u5e72\u7684\u4e8b\uff08\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\u662f\u8f93\u51fa\uff09\uff0c\u4ece\u800c\u4f7f\u5f97\u7a0b\u5e8f\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<p>\u5bf9\u4e8efor\uff0c\u5c31\u8fd9\u4e48\u591a\u4e1c\u897f\u8981\u8bf4\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86while\uff0c\u90a3\u4e48\u7406\u89e3for\u8d77\u6765\u4e5f\u662f\u6bd4\u8f83\u8f7b\u677e\u7684<\/p>\n<p>\u53ef\u80fd\u4f1a\u6709\u4e9b\u4eba\u7ea0\u7ed3\uff1a\u5728\u5565\u65f6\u5019\u7528while\uff0c\u5565\u65f6\u5019\u7528for\uff1f<\/p>\n<p>\u7b54\u6848\u5c31\u662f\u5728\u4f60\u60f3\u7528while\u7684\u65f6\u5019\u7528while\uff0c\u5728\u4f60\u60f3\u7528for\u7684\u65f6\u5019\u7528for<\/p>\n<p>\u56e0\u4e3a\u8fd9\u4e24\u79cd\u5faa\u73af\u90fd\u662f\u53ef\u4ee5\u76f8\u4e92\u8f6c\u6362\u7684\uff0c\u5c31\u50cf\u4f60\u53ef\u4ee5\u9009\u62e9\u722c\u697c\u68af\u8fd8\u662f\u4e58\u7535\u68af\u4e0a\u697c\u4e00\u6837<\/p>\n<p>\u6700\u540e\uff0c\u7559\u4e0b\u4e00\u4e2a\u7ec3\u4e60\uff0c\u8bf7\u5c06\u4e0b\u9762\u7684while\u5faa\u73af\u6539\u4e3afor\u5faa\u73af\uff08\u5176\u5b9e\u5c31\u662f3.3\u91cc\u9762\u7684\u4ee3\u7801\u5566\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\nint main()\r\n{\r\n    double Fahrenheit = 0, Celsius = 0;\r\n    printf(&quot;\u6444\u6c0f\u5ea6\\t\u534e\u6c0f\u5ea6:\\n&quot;);\r\n \r\n    while (Fahrenheit &lt;= 100)\r\n    {\r\n        Celsius = (Fahrenheit - 32)\/1.8;   \/\/\u5c06\u534e\u6c0f\u5ea6\u6362\u7b97\u6210\u6444\u6c0f\u5ea6\r\n        printf(&quot;%.2f\\t%.2f\\n&quot;, Fahrenheit, Celsius);\r\n        Fahrenheit = Fahrenheit + 10;\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728c\u8bed\u8a00\u4e2d\uff0c\u4e00\u5171\u67093\u79cd\u5faa\u73af\u7ed3\u6784\uff0c\u4e00\u79cd\u662f\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86\u7684while\uff0c\u5176\u4ed6\u4e24\u79cd\u5219\u662ffor\u548cdo&#8230;whi\u2026 <span class=\"read-more\"><a href=\"https:\/\/yangwang.hk\/?p=574\">Read More: 3.8\u3001for\u5faa\u73af &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","hentry","category-c"],"views":3010,"_links":{"self":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=574"}],"version-history":[{"count":4,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/yangwang.hk\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yangwang.hk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}