{"id":16,"date":"2007-05-21T00:01:31","date_gmt":"2007-05-21T04:01:31","guid":{"rendered":"http:\/\/prglab.com\/blog\/?page_id=16"},"modified":"2007-08-25T13:48:07","modified_gmt":"2007-08-25T17:48:07","slug":"js-loop","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-loop","title":{"rendered":"JavaScript\u4e2d\u7684\u5faa\u73af"},"content":{"rendered":"<p>\u5faa\u73af\u8bed\u53e5\u53ef\u7528\u6765\u53cd\u590d\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fbe\u5230\u81f3\u6b64\u7684\u6b21\u6570\u6216\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nJavaScript \u5faa\u73af<\/p>\n<p>\u5728\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\u6211\u4eec\u7ecf\u5e38\u78b0\u5230\u60f3\u8981\u8fde\u7eed\u53cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u7684\u60c5\u51b5\u3002 \u4e0e\u5176\u591a\u6b21\u91cd\u590d\u5199\u51e0\u4e4e\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u53d6\u800c\u4ee3\u4e4b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u6765\u5b8c\u6210\u8fd9\u6837\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5728 JavaScript\u4e2d\uff0c\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5faa\u73af\uff1a<\/p>\n<ul>\n<li><strong>for<\/strong> &#8211; \u5c06\u4e00\u6bb5\u4ee3\u7801\u6267\u884c\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u7684\u6b21\u6570\u3002<\/li>\n<li><strong>while<\/strong> &#8211; \u53ea\u8981\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\u5c31\u6301\u7eed\u5faa\u73af\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong> for \u5faa\u73af<\/strong><\/p>\n<p>\u901a\u5e38\u5f53\u4f60\u77e5\u9053\u4e00\u6bb5\u811a\u672c\u9700\u8981\u88ab\u6267\u884c\u591a\u5c11\u6b21\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528for\u5faa\u73af\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">for (var=\u521d\u59cb\u503c;var&lt;=\u7ed3\u675f\u503c;var=var+\u589e\u91cf)<br \/>\n{<br \/>\n\u8981\u6267\u884c\u7684\u4ee3\u7801<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u4f8b\u5b50<\/strong><br \/>\n\u89e3\u91ca\uff1a\u4ee5\u4e0b\u4f8b\u5b50\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5faa\u73af\uff0c\u4ee5i=0\u5f00\u59cb\uff0c\u53ea\u8981 i \u5c0f\u4e8e\u6216\u7b49\u4e8e10\uff0c\u5faa\u73af\u5c31\u7ee7\u7eed\u8fdb\u884c\uff0c\u6bcf\u6267\u884c\u4e00\u6b21\uff0ci \u589e\u52a0 1 \uff0c\u76f4\u5230 i \u8d85\u8fc7 10 \uff0c\u5faa\u73af\u505c\u6b62\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u589e\u91cf\u4e5f\u53ef\u4ee5\u4f7f\u8d1f\u503c\uff0c \u800c\u4e14 &lt;= \u53f7\u7684\u5730\u65b9\u53ef\u4ee5\u662f\u4efb\u4f55\u6bd4\u8f83\u8bed\u53e5\u3002<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar i=0<br \/>\nfor (i=0;i&lt;=10;i++)<br \/>\n{<br \/>\ndocument.write(&#8220;The number is &#8221; + i)<br \/>\ndocument.write(&#8220;&lt;br \/&gt;&#8221;)<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u6267\u884c\u7ed3\u679c<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">The number is 0<br \/>\nThe number is 1<br \/>\nThe number is 2<br \/>\nThe number is 3<br \/>\nThe number is 4<br \/>\nThe number is 5<br \/>\nThe number is 6<br \/>\nThe number is 7<br \/>\nThe number is 8<br \/>\nThe number is 9<br \/>\nThe number is 10<\/td>\n<\/tr>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n<strong>while \u5faa\u73af<\/strong><\/p>\n<p>\u901a\u5e38\u5f53\u9700\u8981\u5728\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\u60c5\u51b5\u4e0b\u6301\u7eed\u53cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u65f6\u4f7f\u7528while\u5faa\u73af\u3002<br \/>\n<strong>\u8bed\u6cd5<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">while (var&lt;=endvalue)<br \/>\n{<br \/>\ncode to be executed<br \/>\n}<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> \u5176\u4e2d &lt;= \u53ef\u4ee5\u662f\u4efb\u4f55\u6bd4\u8f83\u8bed\u53e5\u3002<\/p>\n<p><strong>\u4f8b\u5b50<\/strong><br \/>\n\u89e3\u91ca\uff1a\u4ee5\u4e0b\u4f8b\u5b50\u540c\u6837\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4ee5 i=0 \u5f00\u59cb\u7684\u5faa\u73af\u3002 \u53ea\u8981 i \u5c0f\u4e8e\u7b49\u4e8e10 \u5faa\u73af\u5c31\u6301\u7eed\u6267\u884c\u3002\u6bcf\u6267\u884c\u4e00\u6b21\uff0ci\u503c\u589e\u52a01\u3002<\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar i=0<br \/>\nwhile (i&lt;=10)<br \/>\n{<br \/>\ndocument.write(&#8220;The number is &#8221; + i)<br \/>\ndocument.write(&#8220;&lt;br \/&gt;&#8221;)<br \/>\ni=i+1<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u6267\u884c\u7ed3\u679c<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">The number is 0<br \/>\nThe number is 1<br \/>\nThe number is 2<br \/>\nThe number is 3<br \/>\nThe number is 4<br \/>\nThe number is 5<br \/>\nThe number is 6<br \/>\nThe number is 7<br \/>\nThe number is 8<br \/>\nThe number is 9<br \/>\nThe number is 10<\/td>\n<\/tr>\n<\/table>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n<strong> do&#8230;while \u5faa\u73af<\/strong><\/p>\n<p>do&#8230;while \u5faa\u73af\u662fwhile\u5faa\u73af\u7684\u4e00\u4e2a\u53d8\u4f53\u3002\u8fd9\u79cd\u5faa\u73af\u5c06\u81f3\u5c11\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u4e00\u6b21\uff0c\u7136\u540e\u518d\u68c0\u9a8c\u6307\u5b9a\u6761\u4ef6\u662f\u5426\u4e3a\u771f\uff0c \u5728\u6761\u4ef6\u4e3a\u771f\u7684\u60c5\u51b5\u4e0b\u53cd\u590d\u6267\u884c\u4ee3\u7801\u3002<br \/>\n\u6761\u4ef6\u68c0\u9a8c\u5728\u7b2c\u4e00\u6b21\u5faa\u73af\u4f53\u4ee3\u7801\u6267\u884c\u4e4b\u540e\uff0c\u6240\u4ee5\u5faa\u73af\u4f53\u4ee3\u7801\u603b\u662f\u4f1a\u88ab\u6267\u884c\u81f3\u5c11\u4e00\u6b21\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">do<br \/>\n{<br \/>\ncode to be executed<br \/>\n}<br \/>\nwhile (var&lt;=endvalue)<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u4f8b\u5b50<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar i=0<br \/>\ndo<br \/>\n{<br \/>\ndocument.write(&#8220;The number is &#8221; + i)<br \/>\ndocument.write(&#8220;&lt;br \/&gt;&#8221;)<br \/>\ni=i+1<br \/>\n}<br \/>\nwhile (i&lt;0)<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/td>\n<\/tr>\n<\/table>\n<p><strong>\u6267\u884c\u7ed3\u679c<\/strong><\/p>\n<table align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"96%\">\n<tr>\n<td class=\"quote\">The number is 0<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u5faa\u73af\u8bed\u53e5\u53ef\u7528\u6765\u53cd\u590d\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u8fbe\u5230\u81f3\u6b64\u7684\u6b21\u6570\u6216\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- JavaScript \u5faa\u73af \u5728\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\u6211\u4eec\u7ecf\u5e38\u78b0\u5230\u60f3\u8981\u8fde\u7eed\u53cd\u590d\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\u7684\u60c5\u51b5\u3002 \u4e0e\u5176\u591a\u6b21\u91cd\u590d\u5199\u51e0\u4e4e\u76f8\u540c\u7684\u4ee3\u7801\uff0c\u53d6\u800c\u4ee3\u4e4b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u6765\u5b8c\u6210\u8fd9\u6837\u7684\u529f\u80fd\u3002 \u5728 JavaScript\u4e2d\uff0c\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u5faa\u73af\uff1a for &#8211; \u5c06\u4e00\u6bb5\u4ee3\u7801\u6267\u884c\u5faa\u73af\u6267\u884c\u4e00\u5b9a\u7684\u6b21\u6570\u3002 while &#8211; \u53ea\u8981\u6307\u5b9a\u6761\u4ef6\u4e3a\u771f\u5c31\u6301\u7eed\u5faa\u73af\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; for \u5faa\u73af \u901a\u5e38\u5f53\u4f60\u77e5\u9053\u4e00\u6bb5\u811a\u672c\u9700\u8981\u88ab\u6267\u884c\u591a\u5c11\u6b21\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528for\u5faa\u73af\u3002 \u8bed\u6cd5 for (var=\u521d\u59cb\u503c;var&lt;=\u7ed3\u675f\u503c;var=var+\u589e\u91cf) { \u8981\u6267\u884c\u7684\u4ee3\u7801 } \u4f8b\u5b50 \u89e3\u91ca\uff1a\u4ee5\u4e0b\u4f8b\u5b50\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5faa\u73af\uff0c\u4ee5i=0\u5f00\u59cb\uff0c\u53ea\u8981 i \u5c0f\u4e8e\u6216\u7b49\u4e8e10\uff0c\u5faa\u73af\u5c31\u7ee7\u7eed\u8fdb\u884c\uff0c\u6bcf\u6267\u884c\u4e00\u6b21\uff0ci \u589e\u52a0 1 \uff0c\u76f4\u5230 i \u8d85\u8fc7 10 \uff0c\u5faa\u73af\u505c\u6b62\u3002 \u6ce8\u610f\uff1a \u589e\u91cf\u4e5f\u53ef\u4ee5\u4f7f\u8d1f\u503c\uff0c \u800c\u4e14 &lt;= \u53f7\u7684\u5730\u65b9\u53ef\u4ee5\u662f\u4efb\u4f55\u6bd4\u8f83\u8bed\u53e5\u3002 &lt;html&gt; &lt;body&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; var i=0 for (i=0;i&lt;=10;i++) { document.write(&#8220;The number is &#8221; + i) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":9,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/16"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/16\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}