Google

loops

Loops are used to execute a segment of code repeatedly until some condition is met. JavaScript's looping constructs are : while - do...while - for.

 

The while Loop

The while statement executes its statement block as long as the expression after the while evaluates to true, that is . If the condition never changes and is true, the loop will iterate forever (infinite loop). If the condition is false control goes to the statement right after the closing curly brace of the loop's statement block.

 Format :


while (condition) {
statements;
increment/decrement counter;
 
 

The do/while Loop

The do/while statement executes a block of statements repeatedly until a condition becomes false.  this loop executes the statements in the body of the loop at least once before testing its expression .

 Format :


do
{ statements;}
while (condition); 
 
 

The for Loop

The for loop consists of the for keyword followed by three expressions separated by semicolons and enclosed within parentheses. 
The first expression is used to set the initial value of variables and is executed just once, the second expression is used to test whether the loop should continue or stop, and the third expression updates the loop variables; that is, it increments or decrements a counter, which will usually determine how many times the loop is repeated .

Format :


for(Expression1;Expression2;Expression3) 
{
statement(s);



No comments: