DEV Community

Kavin Loyola S
Kavin Loyola S

Posted on

JS Looping

While
It repeatedly executes block of codes until the specified condition is true.

for Example:

Divisors of given number ?

let givenNo=15;
i=1;
while(i<=givenNo)
{
   if(givenNo%i==0){
     console.log(i);
   }
i++;
}

o/p - 1 3 5 15

Enter fullscreen mode Exit fullscreen mode

For

it repeatedly executes a block of code as long as a specified condition is true, using initialization, condition, and increment/decrement expressions.

for example:

for(let i=1;i<=7;i++)
{
   console.log(i);
}

o/p - 1 2 3 4 5 6 7

Enter fullscreen mode Exit fullscreen mode

do - while

it executes a block of code at least once and then continues looping as long as a specified condition is true.

let i=5;
do{
   console.log("Hello");
   i++;
}while(i<5);

o/p - Hello

Enter fullscreen mode Exit fullscreen mode

Top comments (0)