JavaScript----循环语句

1. 循环语句的介绍

循环语句就是让一部分代码重复执行,javascript中常用的循环语句有:

  • for
  • while
  • do-while

2. for循环

javascript 复制代码
var array = [1, 4, 5];

for(var index = 0; index < array.length; index++){
    result = array[index];
    alert(result);
}

3. while循环

javascript 复制代码
var array = [1, 4, 5];        
var index = 0;

while (index < array.length) {
    result = array[index];
    alert(result);
    index++;
}

说明:

当条件成立的时候, while语句会循环执行

4. do-while循环

javascript 复制代码
var array = [1, 4, 5];
var index = 0;

do {
    result = array[index];
    alert(result);
    index++;
} while (index < array.length);

说明:

当条件不成立的时候do语句也会执行一次

5. 小结

  • js中循环语句有:
    • for
    • while
    • do-while
相关推荐
米啦啦.13 小时前
STL(标准模板库)
开发语言·c++·stl
lly20240613 小时前
建造者模式:构建复杂对象的最佳实践
开发语言
A923A13 小时前
【javaScript 原型精讲】
javascript·原型·原型链
无尽冬.13 小时前
个人八股之string字符串
java·开发语言·经验分享·后端·异世界
卷帘依旧13 小时前
手写throttle
javascript
吃好睡好便好13 小时前
在Matlab中绘制抛物三维曲面图
开发语言·人工智能·学习·算法·matlab·信息可视化
半步仙人13 小时前
MATLAB的几种取整操作总结
开发语言·matlab
伯远医学13 小时前
Nat. Methods | 邻近标记技术:活细胞中捕捉分子互作的新利器
java·开发语言·前端·javascript·人工智能·算法·eclipse
wjs202413 小时前
Matplotlib 轴标签和标题
开发语言
研☆香13 小时前
es6的新特性介绍
前端·ecmascript·es6