0

JavaScript for 循环语句

Posted by 撒得一地 on 2016年5月4日 in JavaScript教程

For 循环是最紧凑的一种循环形式。它包括以下三个重要部分:

初始化化:对循环计数器起始值进行初始化。初始化语句在循环开始之前被执行。

检测语句:测试语句将测试被给定的条件是真还是假。如果条件为 true,然后在循环内的代码将会被执行,否则就跳出循环。

迭代语句:在迭代语句中你可以增加或减少你的计数器的值。

你可以把上面的三个部分,用分号隔开,放在同一个代码行里。

流程图

在 JavaScript 中 for 循环流程图如下:

JavaScript 中 for 循环流程图

语法

JavaScript 下 for 循环的语法格式如下:

for (initialization; test condition; iteration statement){
   Statement(s) to be executed if test condition is true
}

示例

试试下面的示例,学习如何在 JavaScript 中使用 for 循环。

<html>
  <body>
   <script type="text/javascript">
     <!--
        var count;
        document.write("Starting Loop" + "<br />");

       for(count = 0; count < 10; count++){
           document.write("Current Count : " + count );
           document.write("<br />");
       }
         
      document.write("Loop stopped!");

    //-->
  </script>

    <p>Set the variable to different value and then try...</p>
  </body>
</html>

亲自试一试

Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped! 
Set the variable to different value and then try...

上一篇:

下一篇:

相关推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注

6 + 1 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.