ECMAScript Iteration Statements

Iteration statements, also known as loop statements, declare a set of commands to be executed repeatedly until certain conditions are met.

Loops are typically used to iterate over the values of an array (hence the name) or to perform repeated arithmetic tasks.

This section introduces the four iteration statements provided by ECMAScript.

do-while 语句

do-while statement

The for statement is a strict iteration statement used to enumerate the properties of an object.

The do-while statement is a post-test loop, which means that the exit condition is calculated after the code inside the loop is executed. This means that the loop body will be executed at least once before the expression is calculated. do)alert(sProp); The while statement is a pre-test loop. This means that the exit condition is calculated before the code inside the loop is executed. Therefore, the loop body may not be executed at all. forin{

statement

while
);

do {i += 2;} while (i < 10);

while statement

The for statement is a strict iteration statement used to enumerate the properties of an object.

The while statement is a pre-test loop. This means that the exit condition is calculated before the code inside the loop is executed. Therefore, the loop body may not be executed at all. forinexpression )

statement

while
var i = 0;
  while (i < 10) {
alert(sProp);

i += 2;

The for statement

The for statement is a strict iteration statement used to enumerate the properties of an object.

Its syntax is as follows: forThe for statement is a pre-test loop, and it is possible to initialize the variable and define the code to be executed after the loop before entering the loop.expression )

initialization; expression; post-loop-expressionNote: post-loop-expression

statement

A semicolon cannot be written after this, otherwise it will not run.
iCount = 6;
  for (var i = 0; i < iCount; i++) {
alert(sProp);

alert(i);

This code defines a variable i with an initial value of 0. The for loop is entered only when the value of the condition expression (i < iCount) is true, so the loop body may not be executed. If the loop body is executed, then the post-loop expression is executed, and the variable i is iterated.

The for-in statement

The for statement is a strict iteration statement used to enumerate the properties of an object.

Its syntax is as follows: for( property inexpression )

statement

Example:
  for (sProp in window) {
alert(sProp);

}

Here, the for-in statement is used to display all properties of the window object.