Comparing For and While If you have read the previous chapter, about the for loop, you will discover that a while loop is much the same as a for loop, with statement 1 and statement 3 omitted. The loop in this example uses aforloop to collect the car names from the cars array: ...
别忘记增加条件中所用变量的值,否则循环永远不会结束! 比较for 和 while 如果您已经阅读了前面那一章关于 for 循环的内容,您会发现 while 循环与 for 循环很像。 for 语句实例 本例中的循环使用 for 循环来显示 cars 数组中的所有值: cars=["BMW","Volvo","Saab","Ford"]; var i=0; for (;cars[i...
JS 放置 JS 语句 JS 注释 JS 变量 JS 运算符 JS 比较 JS If...Else JS Switch JS 消息框 JS 函数 JS For Loop JS While Loop JS Break Loops JS For...In JS 事件 JS Try...Catch JS Throw JS onerror JS 特殊字符 JS 指导方针 JavaScript 对象 JS 对象简介 JS 字符串 JS 日期 JS 数组 JS ...
The difference between while and do...while is that the do...while loop executes its body at least once. For example, let i = 0; // false condition // body executes once do { console.log(i); } while (i > 1); // Output: 0 Run Code On the other hand, the while loop doesn...
The switch conditional The for loop The while loop Introduction Perhaps, one of the most paramount aspects of modern-day programming is that of control flow. This chapter is devoted briefly exploring the different kinds of control flow constructs in JavaScript. But before that, let's quickly unde...
JS For JS Break 只要指定条件为 true,循环就可以一直执行代码。while 循环 While 循环会在指定条件为真时循环执行代码块。 语法 while (条件) { 需要执行的代码 } 实例 本例中的循环将继续运行,只要变量 i 小于 5: while (i<5) { x=x + "The number is " + i + ""; i++; } 亲自试一试...
JS 放置 JS 语句 JS 注释 JS 变量 JS 运算符 JS 比较 JS If...Else JS Switch JS 消息框 JS 函数 JS For Loop JS While Loop JS Break Loops JS For...In JS 事件 JS Try...Catch JS Throw JS onerror JS 特殊字符 JS 指导方针 JavaScript 对象 JS 对象简介 JS 字符串 JS 日期 JS 数组 JS ...
JS 对象 JS HTML DOM JavaScript While 循环Previous Page Next Page JavaScript 中的循环用来将同一段代码执行指定的次数(或者当指定的条件为 true 时)。实例 While 循环 利用while 循环在指定条件为 true 时来循环执行代码。 Do while 循环 利用do...while 循环在指定条件为 true 时来循环执行代码。在即使条件...
JavaScriptdo...whileLoop: Example Let's take an example and see thedo...whileloop in action. In this tutorial, we explained thewhileanddo...whileloops used in the JavaScript. ← JavaScript for Loop JS Switch case → Try our new interactive courses. ...
比较For 与 While 如果您已经阅读了之前关于循环的章节,您会发现 while 循环与 for 循环相当类似,其中的语句 1 和 语句 2 都可以省略。 本例中的循环使用 for 循环来提取 cars 数组中的汽车品牌: 实例 var cars = ["BMW", "Volvo", "Saab", "Ford"]; var i = 0; var text = ""; for (;cars...