TypeScript while循环
2019-01-03 16:57 更新
TypeScript While循环
每次指定的条件求值为true时,while循环都会执行指令。换句话说,循环在执行代码块之前评估条件。
语法
while(condition) {
// statements if the condition is true
}
流程图
示例:while循环
var num:number = 5;
var factorial:number = 1;
while(num >=1) {
factorial = factorial * num;
num--;
}
console.log("The factorial is "+factorial);
上面的代码片段使用while循环来计算变量num中值的阶乘。
在编译时,它将生成以下JavaScript代码:
//Generated by typescript 1.8.10
var num = 5;
var factorial = 1;
while (num >= 1) {
factorial = factorial * num;
num--;
}
console.log("The factorial is " + factorial);
它产生以下输出:
The factorial is 120
以上内容是否对您有帮助:

免费 AI IDE


更多建议: