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
相关推荐
成都渲染101云渲染66663 小时前
如何在3ds Max中实现更快、更高质量的渲染
前端·javascript·人工智能
哥不想学算法5 小时前
【C++】字符串字面量拼接
开发语言·c++
gugucoding6 小时前
21. 【C语言】打包不同类型:结构体
c语言·开发语言
Brookty7 小时前
【JavaEE】线程安全(一).4:写块串行保安全、CAS
java·开发语言·java-ee·多线程·线程安全
SRET7 小时前
Mineradio 沙盒隔离安装完全指南 | 一键安全运行 Electron 应用!!!
javascript·经验分享·安全·electron·aigc·音视频·ai编程
全栈前端老曹7 小时前
【MongoDB】安全与权限管理 —— 用户认证、角色权限、SSL 加密
前端·javascript·数据库·安全·mongodb·nosql·ssl
F20226974867 小时前
西门子 PLC 与 C# 通信
开发语言·c#
gugucoding8 小时前
31. 【C语言】堆栈与队列的实现
c语言·开发语言·数据结构·链表
万联WANFLOW8 小时前
多分支企业组网,IP 网段到底该怎么规划
开发语言·php