📜  ES6-循环

📅  最后修改于: 2020-10-25 10:30:00             🧑  作者: Mango


有时,某些指令需要重复执行。循环是执行此操作的理想方法。循环代表一组必须重复的指令。在循环的上下文中,重复称为迭代

下图说明了循环的分类-

循环

定环

迭代次数是确定/固定的循环称为确定循环。 “ for循环”是确定循环的实现。

for (initial_count_value; termination-condition; step) { 
   //statements
}   
Sr.No Definite Loop & Description
1 The ‘for’ loop

The for loop executes the code block for a specified number of times.

2 The for…in loop

The for…in loop is used to loop through an object’s properties.

3 The for…of loop

The for…of loop is used to iterate iterables instead of object literals.

无限循环

当循环中的迭代次数不确定或未知时,将使用不确定循环。

无限循环可以使用-

Sr.No Indefinite Loop & Description
1 The while loop

The while loop executes the instructions each time the condition specified evaluates to true.

2 The do…while loop

The do…while loop is similar to the while loop except that the do…while loop doesn’t evaluate the condition for the first time the loop executes.

循环控制语句

Sr.No Loop Control Statements & Description
1 The break statement

The break statement is used to take the control out of a construct.

2 The continue statement

The continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop.

使用标签控制流程

标签只是标识符,后跟一个冒号(:),该冒号应用于语句或代码块。标签可以与休息使用,并继续更精确地控制流量。

“ continue”“ break”语句及其标签名称之间不允许换行。同样,标签名称和关联的循环之间不应有任何其他语句

Sr.No Label & Description
1 Label with Break

A label can be used with break and continue to control the flow more precisely.

2 Label with Continue

Line breaks are not allowed between the ‘continue’ or ‘break’ statement and its label name.