You are currently viewing Loops in JavaScript

Loops in JavaScript

Loops in JavaScript

Loops in JavaScript

Loops in JavaScript are used to repeat a piece of code until a specified condition is met. This loop concept is used in all programming languages like Python, Java, C, and C++. The concept is mainly the same everywhere but they differ in syntax wise.

Types of Loops

There are mainly three types of Loops in JS

  • For Loop
  • While Loop
  • Do While Loop

Why do we use Loop?

Suppose we want to print “CodeShruta ” 100 times. If we do this process manually it will take a lot of time. We need to write 100 print statements. But with the help of a loop, we can print it by using 1 print statement.

Let’s say we print CodeShruta 5 times.

Without Looping Concept[Iterative Method]

console.log("CodeShruta");
console.log("CodeShruta");
console.log("CodeShruta");
console.log("CodeShruta");
console.log("CodeShruta");

With Looping Concept[Loop Method]


for (let i =0 ; i < 5; i++){
    console.log("CodeShruta");
}

For Loop

Syntax

for( initialization ; condition ; increment/decrement)
{
//Body of for loop
}

For loop consist of four parts:

  • Initialization

In the initialization part, we initialize the variable which is used to check for the condition. We can use an already initialized variable or we can declare inside the initialization part which is local to for loop.

  • Condition

Condition is used to make an entry in the body of the loop. If the condition is true we can access the body of the for a loop. And if the condition is false we will exit from the loop.

  • Body of for loop

After the second part, we will access the Body of the loop. If the condition returns true then we execute the statement inside for loop.

  • increment/decrement

In this part, we will update the variable for the next iteration.

After the increment/decrement part, the condition will again check against the updated variable and then the body will be executed if the condition returns true. And this process will be repeated until the condition becomes false.

Flowchart

Example

//JavaScript Program to print sum of n natural no.

let sum = 0;
const n = 50

for (let i = 1; i <= n; i++)
{
    sum = sum + i; 
}

console.log('sum of first',n,'natural no.is', sum);

While Loop

While loop is kind of a repeated if statement. The loop stops its execution when the condition becomes false.

Syntax

while (condition)
{
   //loop body
}

Flowchart

Example

//Print number from 1 to 10 using while loop

let i = 1;
const n = 10;

while (i <= n) {
    console.log(i);
    i = i + 1;
}
// when i become greater then n ,then while loop  will stops its execution.

Do While Loop

Do while Loop is also known as Exit Controlled loops. It means the loop body will execute at least once before evaluating the condition. If the condition returns true, the body of the loop inside the do statement will be executed again.

Syntax

do
{
//Body of do while loop
}while(condition);

Flowchart

Example

//Print number from 1 to 10 using do while loop
let i = 1;
const n = 10;

do {
    console.log(i);
    i++;
} 
while(i <= n);

Loops in JavaScript are very useful and this concept can be used to make programmers life easier.

ps : Check this also Conditional Statements in JavaScript, Check all CSS tutorials to make your website more attractive .

Article Contributed By : Ankita Kataria.

Thank you !!

This Post Has One Comment

Leave a Reply