{"id":52385,"date":"2024-12-03T09:06:18","date_gmt":"2024-12-03T01:06:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52385\/"},"modified":"2024-12-03T09:06:18","modified_gmt":"2024-12-03T01:06:18","slug":"%e6%8e%8c%e6%8f%a1-javascript-%e4%b8%ad%e7%9a%84%e5%be%aa%e7%8e%af%ef%bc%9awhile%e3%80%81dowhile-%e5%92%8c-for","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52385\/","title":{"rendered":"\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732081891673d78e34c4af.jpg\" class=\"aligncenter\" title=\"\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`\u63d2\u56fe\" alt=\"\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 javascript \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u5faa\u73af\uff1a<strong>while<\/strong>\u3001<strong>do&#8230;while<\/strong> \u548c <strong>for<\/strong>\u3002\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u5982\u4f55\u8df3\u51fa\u5faa\u73af\u3001\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\u4ee5\u53ca\u4f7f\u7528\u6807\u7b7e\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u63a7\u5236\u6d41\u3002\u8ba9\u6211\u4eec\u6f5c\u5165\u5427\uff01<\/p>\n<h3> <strong>while \u5faa\u73af<\/strong> <\/h3>\n<p>\u53ea\u8981\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\uff0c<strong>while<\/strong> \u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>while (condition) {\n  \/\/ code to execute\n}\n\n<\/pre>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let count = 0;\n\nwhile (count &lt; 5) {\n  console.log(count);\n  count++;\n}\n\/\/ output: 0 1 2 3 4\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u53ea\u8981 <strong>count<\/strong>&nbsp;\u5c0f\u4e8e 5\uff0c\u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<li>\u5728\u5faa\u73af\u5185\uff0c<strong>count<\/strong> \u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u52a0 1\u3002<\/li>\n<\/ul>\n<h3> <strong>do&#8230;while \u5faa\u73af<\/strong> <\/h3>\n<p><strong>do&#8230;while<\/strong> \u5faa\u73af\u786e\u4fdd\u5728\u68c0\u67e5\u6761\u4ef6\u4e4b\u524d\u4ee3\u7801\u5757\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>do {\n  \/\/ code to execute\n} while (condition);\n\n<\/pre>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>let count = 0;\n\ndo {\n  console.log(count);\n  count++;\n} while (count &lt; 5);\n\/\/ output: 0 1 2 3 4\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5faa\u73af\u786e\u4fdd\u4ee3\u7801\u5757\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<li>\u7b2c\u4e00\u6b21\u8fed\u4ee3\u540e\u68c0\u67e5\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<h3> <strong>for \u5faa\u73af<\/strong> <\/h3>\n<p>\u5f53\u60a8\u9884\u5148\u77e5\u9053\u8981\u6267\u884c\u4e00\u6761\u8bed\u53e5\u6216\u8bed\u53e5\u5757\u591a\u5c11\u6b21\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>for<\/strong> \u5faa\u73af\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>for (initialization; condition; increment) {\n  \/\/ code to execute\n}\n\n<\/pre>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>for (let i = 0; i &lt; 5; i++) {\n  console.log(i);\n}\n\/\/ output: 0 1 2 3 4\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>\u521d\u59cb\u5316<\/strong>\uff1a<strong>\u8ba9 i = 0<\/strong>&nbsp;\u521d\u59cb\u5316\u5faa\u73af\u8ba1\u6570\u5668\u3002<\/li>\n<li> <strong>\u6761\u4ef6<\/strong>\uff1a<strong>i &lt; 5<\/strong>&nbsp;\u662f\u5faa\u73af\u7ee7\u7eed\u7684\u6761\u4ef6\u3002<\/li>\n<li> <strong>increment<\/strong>\uff1a<strong>i++<\/strong> \u6bcf\u6b21\u8fed\u4ee3\u540e\u9012\u589e\u5faa\u73af\u8ba1\u6570\u5668\u3002<\/li>\n<\/ul>\n<h3> <strong>\u6253\u7834\u5faa\u73af<\/strong> <\/h3>\n<p><strong>break<\/strong> \u8bed\u53e5\u7528\u4e8e\u63d0\u524d\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>for (let i = 0; i &lt; 10; i++) {\n  if (i === 5) {\n    break;\n  }\n  console.log(i);\n}\n\/\/ output: 0 1 2 3 4\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 <strong>i<\/strong> \u7b49\u4e8e 5 \u65f6\uff0c\u5faa\u73af\u9000\u51fa\u3002<\/li>\n<\/ul>\n<h3> <strong>\u7ee7\u7eed\u4e0b\u4e00\u4e2a\u8fed\u4ee3<\/strong> <\/h3>\n<p><strong>\u7ee7\u7eed<\/strong>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u79fb\u81f3\u5faa\u73af\u7684\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>for (let i = 0; i &lt; 10; i++) {\n  if (i === 5) {\n    continue;\n  }\n  console.log(i);\n}\n\/\/ output: 0 1 2 3 4 6 7 8 9\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 <strong>i<\/strong> \u7b49\u4e8e 5 \u65f6\uff0c\u5faa\u73af\u4f1a\u8df3\u8fc7\u8fed\u4ee3\uff0c\u5e76\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/li>\n<\/ul>\n<h3> <strong>\u4e2d\u65ad\u548c\u7ee7\u7eed\u7684\u6807\u7b7e<\/strong> <\/h3>\n<p>\u6807\u7b7e\u53ef\u7528\u4e8e\u4ece\u5185\u5faa\u73af\u8df3\u51fa\u6216\u7ee7\u7eed\u5916\u5faa\u73af\u7684\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>outerloop: for (let i = 0; i &lt; 3; i++) {\n  for (let j = 0; j &lt; 3; j++) {\n    if (i === 1 &amp;&amp; j === 1) {\n      break outerloop;\n    }\n    console.log(`i = ${i}, j = ${j}`);\n  }\n}\n\/\/ output: i = 0, j = 0\n\/\/         i = 0, j = 1\n\/\/         i = 0, j = 2\n\/\/         i = 1, j = 0\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 <strong>i<\/strong> \u7b49\u4e8e 1 \u4e14 <strong>j<\/strong> \u7b49\u4e8e 1 \u65f6\uff0c<strong>break outerloop<\/strong> \u8bed\u53e5\u9000\u51fa\u5916\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p><strong>\u7ee7\u7eed\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>outerLoop: for (let i = 0; i &lt; 3; i++) {\n  for (let j = 0; j &lt; 3; j++) {\n    if (i === 1 &amp;&amp; j === 1) {\n      continue outerLoop;\n    }\n    console.log(`i = ${i}, j = ${j}`);\n  }\n}\n\/\/ Output: i = 0, j = 0\n\/\/         i = 0, j = 1\n\/\/         i = 0, j = 2\n\/\/         i = 1, j = 0\n\/\/         i = 2, j = 0\n\/\/         i = 2, j = 1\n\/\/         i = 2, j = 2\n\n<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5f53 <strong>i<\/strong> \u7b49\u4e8e 1 \u4e14 <strong>j<\/strong> \u7b49\u4e8e 1 \u65f6\uff0c<strong>continue outerloop<\/strong> \u8bed\u53e5\u4f1a\u8df3\u8fc7\u5916\u5faa\u73af\u7684\u5f53\u524d\u8fed\u4ee3\u3002<\/li>\n<\/ul>\n<h3> <strong>\u603b\u7ed3<\/strong> <\/h3>\n<ul>\n<li> <strong>while loop<\/strong>\uff1a\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\u5c31\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<li> <strong>do&#8230;while loop<\/strong>\uff1a\u786e\u4fdd\u5728\u68c0\u67e5\u6761\u4ef6\u4e4b\u524d\u4ee3\u7801\u5757\u81f3\u5c11\u6267\u884c\u4e00\u6b21\u3002<\/li>\n<li> <strong>for \u5faa\u73af<\/strong>\uff1a\u5f53\u60a8\u9884\u5148\u77e5\u9053\u8981\u6267\u884c\u4e00\u6761\u8bed\u53e5\u6216\u8bed\u53e5\u5757\u591a\u5c11\u6b21\u65f6\u4f7f\u7528\u3002<\/li>\n<li> <strong>break \u8bed\u53e5<\/strong>\uff1a\u63d0\u524d\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li> <strong>\u7ee7\u7eed\u8bed\u53e5<\/strong>\uff1a\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u79fb\u81f3\u5faa\u73af\u7684\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002<\/li>\n<li> <strong>\u6807\u7b7e<\/strong>\uff1a\u7528\u4e8e\u4ece\u5185\u5faa\u73af\u8df3\u51fa\u6216\u7ee7\u7eed\u5916\u5faa\u73af\u7684\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/li>\n<\/ul>\n<h3> <strong>\u7ed3\u8bba<\/strong> <\/h3>\n<p>\u5faa\u73af\u5bf9\u4e8e\u5728 javascript \u4e2d\u6267\u884c\u91cd\u590d\u4efb\u52a1\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u4e86\u89e3\u4e0d\u540c\u7c7b\u578b\u7684\u5faa\u73af\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 <strong>break<\/strong>\u3001<strong>continue<\/strong> \u548c\u6807\u7b7e\u63a7\u5236\u5176\u6d41\u7a0b\uff0c\u60a8\u5c06\u80fd\u591f\u7f16\u5199\u66f4\u9ad8\u6548\u3001\u66f4\u7075\u6d3b\u7684\u4ee3\u7801\u3002\u4e0d\u65ad\u7ec3\u4e60\u548c\u63a2\u7d22\uff0c\u52a0\u6df1\u5bf9 javascript \u5faa\u73af\u7684\u7406\u89e3\u3002<\/p>\n<p>\u8bf7\u7ee7\u7eed\u5173\u6ce8\u6709\u5173 javascript \u7684\u66f4\u591a\u6df1\u5165\u535a\u5ba2\uff01\u5feb\u4e50\u7f16\u7801\uff01<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   win7\u600e\u4e48\u66f4\u6539\u6269\u5c55\u540d win7\u6269\u5c55\u540d\u66f4\u6539\u6559\u7a0b\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for` \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u638c\u63e1 JavaScript \u4e2d\u7684\u5faa\u73af\uff1a`while`\u3001`dowhile` \u548c `for`\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u5728\u672c\u535a\u5ba2\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 javascript \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u5faa\u73af\uff1awhile\u3001do&#8230;while \u548c for\u3002\u6211\u4eec\u8fd8\u5c06\u4ecb\u7ecd\u5982\u4f55\u8df3\u51fa\u5faa\u73af\u3001\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\u4ee5\u53ca\u4f7f\u7528\u6807\u7b7e\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u63a7\u5236\u6d41\u3002\u8ba9\u6211\u4eec\u6f5c\u5165\u5427\uff01 while \u5faa\u73af \u53ea\u8981\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\uff0cwhile \u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u3002 \u8bed\u6cd5\uff1a while (condition) { \/\/ code to execute } \u793a\u4f8b\uff1a let count = 0; while (count &lt; 5) { console.log(count); count++; } \/\/ output: 0 1 2 3 4 \u8bf4\u660e\uff1a \u53ea\u8981 count&nbsp;\u5c0f\u4e8e 5\uff0c\u5faa\u73af\u5c31\u4f1a\u7ee7\u7eed\u6267\u884c\u3002 \u5728\u5faa\u73af\u5185\uff0ccount \u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u52a0 [&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-52385","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52385","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=52385"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52385\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}