确定循环 | 说明 |
for( ; ; ) 循环 | 它执行代码块一定次数。 |
for...in Loop | 它遍历对象的属性。 |
for...of 循环 | 与对象字面量不同,它迭代可迭代对象(数组、字符串等)。 |
for(initialization;condition;incr/decr){ //statement or code to be executed }
初始化: 它是在循环开始时执行一次的初始条件。在这部分,我们初始化变量,或者也可以用于已经初始化的变量。这是一个可选的语句。
条件: 每次都执行它以测试循环的条件。它继续执行循环,直到条件为假。它只返回真或假的布尔值。它也是一个可选语句。
Increment/Decrement: 可以增加或减少变量的值,也是一个可选语句。
语句: 表示循环体,每次都执行,直到条件表达式为假。
使用 for 循环的 2 表。
var i; for(i=1;i<=10;i++) { console.log("2 x "+ i +" =", 2*i); }
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
for 循环 具有多个表达式
您可以使用逗号(, ) 运算符将多个赋值和最终表达式组合在一个 for 循环中.
让我们尝试使用单个 for 循环打印斐波那契数列。
"use strict" for(let temp, a = 0, b = 1; b<40; temp = a, a = b, b = a + temp) console.log(b);
1 1 2 3 5 8 13 21 34
无限for循环
无限for循环的例子如下:
for(;;) { console.log("infinitive loop"); // It will print infinite times }
infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop . . . . infinite loop
for (variable_name in object_name) //Here in is the keyword { // statement or block to execute }
function Mobile(model_no){ this.Model = model_no; this.Color = 'White'; this.RAM = '4GB'; } var Samsung = new Mobile("Galaxy"); for(var props in Samsung) { console.log(props+ " : " +Samsung[props]); }
Model : Galaxy Color : White RAM : 4GB
function Mobile(model_no){ this.Model = model_no; this.Color = 'White'; this.RAM = '4GB'; this.Price = function price() // The loop will give you this function as it is written here. { console.log(this.model + "Price = Rs. 3300"); } } var Samsung = new Mobile("Galaxy"); for(var props in Samsung) { console.log(props+ " : " +Samsung[props]); }
Model : Galaxy Color : White RAM : 4GB Price : function price() { console.log(this.model + "Price = Rs. 3300"); }
for(variable_name of object_name) // Here of is a keyword { //statement or block to execute }
var fruits = ['Apple', 'Banana', 'Mango', 'Orange']; for(let value of fruits) { console.log(value); } /* You can also write the above code like: for(let value of ['Apple', 'Banana', 'Mango', 'Orange']) { console.log(value); } */
Apple Banana Mango Orange
无限循环 | 说明 |
while 循环 | 它每次都执行指令,直到定义的条件评估为真。 |
do...while 循环 | 它与while循环类似,但关键区别在于do…while循环不管终止条件如何都立即执行循环。 |
while (condition) { statements; }
var y = 0; while (y < 4) { console.log(y); y++; }
0 1 2 3
条件 在 while 循环中始终是必需的,因为它是运行循环所必需的。如果条件返回 true,则循环将重新开始,但如果返回 false,则循环将停止。
如果条件始终为真,则循环将永远不会结束。
do { // block of statements to be executed; } while (expression);
var count = 6, fact = 1; do { fact = fact * count--; } while (count > 0); console.log(fact);
720
var count = 6, fact = 1; while (count > 0) { fact = fact * count--; } console.log(fact);
720
循环控制语句 | 说明 |
break 语句 | break 语句用于将程序的控制带出循环。 |
continue 语句 | 它跳过当前迭代的后续语句并将程序的控制带到循环的开始。 |
break;
var n = 1; while(n<=7) { console.log("n="+n); if(n==4) { break; } n++; }
n=1 n=2 n=3 n=4
continue;
var n = 0; while(n<=5) { n++; if(n==3) { continue; } console.log("n = "+n); }
n = 1 n = 2 n = 4 n = 5 n = 6
labelname: Statement
标签 | 说明 |
用break语句标记 | 用于在不使用标签引用的情况下退出循环或从switch语句中退出,但使用标签引用则用于从任何代码块中跳出。 |
带有 continue 语句的标签 | 用于在使用或不使用标签引用的情况下跳过循环的一次迭代。 |
break labelname;
var x, y; loop1: //The first for statement is labeled as "loop1." for (x = 1; x < 4; x++) { loop2: //The second for statement is labelled as "loop2" for (y = 1; y < 4; y++) { if (x === 2 && y === 2) { break loop1; } console.log('x = ' + x + ', y = ' + y); } }
x = 1, y = 1 x = 1, y = 2 x = 1, y = 3 x = 2, y = 1
continue labelname;
var x, y; loop1: //The first for statement is labelled as "loop1" for (x = 1; x < 4; x++) { loop2: //The second for statement is labelled as "loop2" for (y = 1; y < 4; y++) { if (x === 2 && y === 2) { continue loop1; } console.log('x = ' + x + ', y = ' + y); } }
x = 1, y = 1 x = 1, y = 2 x = 1, y = 3 x = 2, y = 1 x = 3, y = 1 x = 3, y = 2 x = 3, y = 3
L: function hello() {}
'use strict'; L: function hello() {} // SyntaxError: In strict mode code, functions can only be declared at top level or inside a block.
L: function* hello() { } // SyntaxError: Generator Functions cannot be labelled