当前位置:嗨网首页>书籍在线阅读

10-while循环

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

3.5.1 while循环

在JavaScript中,最基本的循环是while循环,它检查一个表达式,只要该表达式为true就执行大括号({})的代码,直到该表达式为false为止。

例如,下面的while循环将不断执行,直到i的值为5:

var i = 1;
while (i<5){
  print("Iteration " + i + "\n");
  i++;
}

上述代码的输出如下:

Iteration 1
Iteration 2
Iteration 3
Iteration 4