js while / do while 循环
来源:互联网 发布:英语雅思网络课程 编辑:程序博客网 时间:2024/04/26 16:38
Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.
在JS里循环通常是要某段代码执行一个指定的次数或是在某一特殊条件下执行。
Examples
举例
While loop
How to write a while loop. Use a while loop to run the same block of code while a specified condition is true.
演示怎样写一段while循环
Do while loop
How to write a do…while loop. Use a do…while loop to run the same block of code while a specified condition is true. This loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested.
写do…while循环,(效果和上面的差不多,条件为假的时候也会执行一次,这就是区别,因为判断在执行之后,具体的看连接里的实例)
The while loop
while循环
The while loop is used when you want the loop to execute and continue executing while the specified condition is true.
当条件持续为真的时候循环执行相同的代码, 这就是while循环的用途
while (var<=endvalue){ code to be executed} |
Note: The <= could be any comparing statement.
注意:<=可以比较任何申明(我的理解是不光可以比较数字类型,字符也可以比较)
Example
例子
Explanation: The example below defines a loop that starts with i=0. The loop will continue to run as long as i is less than, or equal to 10. i will increase by 1 each time the loop runs.
解释:下面的例子定位了一个循环,i的初始值为0。当i<=10的时候i会在每次循环后递增1。
<html><body><script type="text/javascript">var i=0while (i<=10){document.write("The number is " + i)document.write("<br />")i=i+1}</script></body></html> |
Result
(运行结果如下)
The number is 0The number is 1The number is 2The number is 3The number is 4The number is 5The number is 6The number is 7The number is 8The number is 9The number is 10 |
The do…while Loop
do…while循环
The do…while loop is a variant of the while loop. This loop will always execute a block of code ONCE, and then it will repeat the loop as long as the specified condition is true. This loop will always be executed at least once, even if the condition is false, because the code is executed before the condition is tested.
do…whlie是另外一种形式的while循环。条件判断在执行之后
do{ code to be executed}while (var<=endvalue) |
Example
例子
<html><body><script type="text/javascript">var i=0do{document.write("The number is " + i)document.write("<br />")i=i+1}while (i<0)</script></body></html> |
Result
(运行后的结果)
The number is 0
- js while / do while 循环
- js的while与do while循环
- while,do while循环
- while、do while、for循环
- swift while、do-while循环
- while,do while循环结构
- 循环:for、while、do-while
- js continue 与 break while 与 do while循环
- while循环、do..while 循环、for循环
- for循环,do....while循环,while循环
- for循环,while循环,do while循环
- Do-While循环
- do while 循环
- do-while循环
- 循环do-while
- do while 循环语句
- 4. do while循环
- do-while循环:
- 感冒
- eclipse在linux下安装
- 练好英语口语的六大法宝
- Asp.net 控件开发中的Render
- 天气预报
- js while / do while 循环
- TD8.0数据库表的字段中文对照
- ARM指令机器码学习——反汇编必学(作者:wogoyixikexie@gliet)
- 修改TestDirector8.0服务器的计算机名后造成客户端无法连接
- TD8.0恢复管理员密码方法
- 初学JAVA的几个问题
- TD8.0项目移植全过程——详解
- JQuery leaning note1
- 兼容FF/IE的insertAdjacentHTML方法