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
相关推荐
Y1rong4 小时前
C++ QT之记事本
开发语言·qt
谎言西西里6 小时前
JS 高手必会:手写 new 与 instanceof
javascript
diegoXie7 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
程序员小白条7 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
liulilittle7 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
天问一8 小时前
使用 Vue Router 进行路由定制和调用的示例
前端·javascript·vue.js
失散138 小时前
Python——1 概述
开发语言·python
萧鼎8 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
小小8程序员8 小时前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++
立志成为大牛的小牛8 小时前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法