JavaScript :: While循环
在编写程序时,您可能会遇到需要一遍又一遍执行某项操作的情况。在这种情况下,您将需要编写循环语句以减少行数。
JavaScript支持所有必要的循环以减轻编程压力。
while循环
JavaScript中最基本的循环是 while 循环,本章将对此进行讨论。while 循环的目的 是在表达式 为真时重复执行语句或代码块 。一旦表达式变为 假, 循环就终止。
流程图
while循环的流程图 如下所示-
语法
JavaScript中while循环的语法 如下-
while (expression) {
Statement(s) to be executed if expression is true
}例子
请尝试以下示例来实现while循环。
Set the variable to different value and then try...
输出
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...
do…while循环
该 做的......而 循环相似, 而 循环,除了健康检查发生在循环的结束。这意味着即使条件为false,循环也将至少执行一次 。
流程图
do-while 循环的流程图 如下-
语法
JavaScript中的do-while循环的语法 如下-
do {
Statement(s) to be executed;
} while (expression);注意 -不要错过do...while 循环结束时使用的分号 。
例子
请尝试以下示例,以了解如何 在JavaScript中实现 do-while循环。
Set the variable to different value and then try...
输出
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Loop Stopped! Set the variable to different value and then try...
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短