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 入住酒店文案祝福语简短