目录

while循环

每次指定的条件求值为true时,while循环都会执行指令。

流程图

而Loop

以下是while循环的语法。

while (expression) {  
   Statement(s) to be executed if expression is true  
} 

例子 (Example)

var num = 5; 
var factorial = 1; 
while(num >= 1) { 
   factorial = factorial * num; 
   num--; 
} 
console.log("The factorial  is "+factorial); 

上面的代码使用while循环来计算变量num中值的阶乘。

成功执行代码后会显示以下输出。

The factorial is 120
↑回到顶部↑
WIKI教程 @2018