JavaScript Loops.

JavaScript Loops.

ยท

1 min read

At times, certain instructions require repeated execution. Loops are an ideal way to do the same. A loop represents a set of instructions that must be repeated. In a loop's context, a repetition is termed as an iteration.

The following figure illustrates the classification of loops-

Screenshot_20210531-001205[1].png

Definite Loop

A loop whose number of iterations are definite/fixed is termed as definite loop. The 'for loop' is an implementation of a definite loop.

for (initial_count_value;
termination-condition;  step) {
//statements
}

Screenshot_20210531-001626[1].png

Indefinite Loop

An indefinite loop is used when the number of iterations in a loop is indeterminate or unknown.

Indefinite loops can be implemented using:

Screenshot_20210531-001643[1].png

The Loop Control Statements

Screenshot_20210531-001659[1].png

Using Labels to control the flow

A Label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code. a label can be used with break and continue to control the flow more precisely.

Line breaks are not allowed between the 'continue' or 'break' statement and its label name. Also, there should not be any statement in between a label name and an associated loop.

Screenshot_20210531-001738[1].png