Looping in JavaScript (For Loop, While & Do While Loops)

Whenever we want to run a chunk code multiple number times, loops are the way to go. In JavaScript there are 2 basic types of loops:

  1. For Loop
    1. For
    2. For..in
    3. For..of
  2. While Loop
    1. While
    2. Do..While

For Loop

In ‘for loop’ there is an index (integer) which gets initialized at the beginning of the loop. Value of that index is either incremented or decremented during the execution of the loop. Iteration of loop continues until the condition to continue the loop remains true.

So, basically, there are 3 parts in a ‘for loop’:

  1. Index initialization (E.g. let i = 0)
  2. Loop continue condition (loop continues until it remains true) (E.g. i <= 10) 
  3. Increment / Decrement (E.g. i++)

Number of iterations of a ‘for loop’ depends on the number of steps taken by the incrementing or decrementing index to reach upper or lower limit specified in the Loop continue condition.  

				
					//Syntax:

for (let index = 0; index < number; index++) {
    //code to iterate
}

/*
Example: 
Find factorial of a number (n)
E.g. !5 = 5 * 4 * 3 * 2 * 1
*/

let n = 6
let f = 1

for(let i = 1; i<=n; i++){
    f = i * f //can also be written as: f*=i
}

console.log("Factorial is: " , f)

/*
Note: 
Here the 'for loop' will iterate until value of 'i' becomes equal to 'n'. 
During each iteration value of 'i' will be multiplied with the value of 'f'
and the resultant will be assigned to 'f'.

Values in 'i' and 'f' during each iteration:
Iteration 1: i = 1, f = 1 * 1 = 1
Iteration 2: i = 2, f = 2 * 1 = 2
Iteration 3: i = 3, f = 3 * 2 = 6
Iteration 4: i = 4, f = 4 * 6 = 24
Iteration 5: i = 5, f = 5 * 24 = 120
Iteration 6: i = 6, f = 6 * 120 = 720
*/
				
			

Nested For Loop

A ‘for loop’ inside the scope of another ‘for loop’.

				
					/*
Exercise:
Create Fibonacci Triangle using nested 'for loop'
 0
 0 1
 0 1 1
 0 1 1 2
 0 1 1 2 3
 0 1 1 2 3 5
*/

let num1
let num2
let num3
let str

for(let i = 1; i<=6; i++){
    num1 = 0
    num2 = 1
    num3 = 0
    str = ""
    for(let j = 1; j<=i; j++){
        str = str + " " + num1
        num3 = num1 + num2
        num2 = num1
        num1 = num3
    }
    console.log(str)
}

/*
Note:
For each iteration of outer for loop all the iterations of inner for loop are done.
Here outer for loop takes care of printing 5 rows and inner for loop takes care of 
finding fibonacci numbers for each row being printed by outer for loop.
*/

				
			

While Loop

while‘ is a conditional loop. It continues the iterations as long as the condition is true.

				
					
let a = 10

while(a < 100){
    console.log(a)
    a += 10
}

/*
Output:
10
20
30
40
50
60
70
80
90

Note: 
The condition (a < 100) is evaluated firts.
If the condition is true then the code inside of the loop is executed.
The iteration of loop continues as long as the condition remains true.
*/
				
			

Do While Loop

do while‘ is also a conditional loop and work similar to ‘while‘ except only one difference that in ‘do while‘ the code inside the loop is executed at least 1 times.

In case of ‘do while‘ the condition is evaluated after the first iteration of loop. 

				
					let a = 100

do{
    console.log(a)
    a-=10
}while(a < 10)

/*
Output:
100

Note:
In 'Do While' Loop, the code inside of the loop is executed first.
Then the condition (a < 10) is evaluated.
If the condition is true then the code inside of the loop is executed again.
The iteration of loop continues as long as the condition remains true.
In the above example the condition (a < 10) is never returns true.
But, still the code inside of the loop gets executed one time.
And we can see 100 printed in the output. 
*/
				
			

Assignment

  • Write a program to to find prime numbers in between 1 and 20.
  • Write a program to print table of 2.
  • Write a program to print all odd number between 1 to 50.
  • Write a program to find sum of first 10 natural numbers.
  • Write a program to find sum of first 10 odd numbers.
  • Write a program to calculate factorial of a given number.
  • Write a program to display square of first 10 numbers.
  • Write a program to find perfect numbers between 1 to 15.
  • Write a program to find armstrong numbers from 1 to 500.
  • Write a program to display following output :       
                *
                **
                ***
                ****
                *****