Loop

Looping refers to the ability of a block of code to repeat itself. Specific number of times until some condition is satisfied.
JavaScript supports three loop control statement are: For loop,while loop and do-while loop.

For loop

The for loop is the basic type of loop and resembles a for in mist other programming language including ANS “C”.

Syntax of the For loop is

For (expression 1; condition ; expression 2)
 {
  // JavaScript statement;
 }

In for loop, expression 1 sets up a counter variable and assigns the initial value. The declaration of the counter variable con also be done here itself. Expression 2 specifies how the initial value expression 1 is incremented/decremented. Condition is a logical expression the determine the number of iteration required.

while loop

The while loop provides a similar functionality. While loop is also known as entry controlled loop.

Syntax of the While loop is

While (condition)
 {
 //JavaScript commands;
 }

In while loop, the condition is a valid JavaScript expression that evaluated to a Boolean value. The JavaScript commands executes as long as the condition is become TRUE. Because the condition is checked before the execution of the body of the loop.

do-while loop

The do-while loop is also known as exit controlled loop as condition is evaluated at the bottom of the loop.

Syntax of the do-while loop is
 
do
 {
 //JavaScript statement;
 }
 while (condition);

In do-while loop, also a set of statement is executed repeatedly until condition become FALSE.

Leave a Reply