Latest posts

10/recent/ticker-posts

Looping Statements in Javascript | For loop | While Loop | Do while Loop

What is meant by Looping Statements?

Looping statements are a type of programming construct that allows a specific set of instructions to be executed repeatedly until a certain condition is met. This is useful when you need to perform a certain operation multiple times, such as when iterating over an array or processing user input.

Looping statements typically have a condition that is evaluated at the beginning of each iteration. If the condition is true, the loop body is executed, and then the condition is evaluated again. This process continues until the condition is false, at which point the loop terminates.

There are several types of looping statements available in programming languages, such as the for loop, while loop, and do-while loop. Each type of loop has its own syntax and is best suited for different scenarios.
Looping statements are an essential part of programming, as they allow you to automate repetitive tasks and perform complex operations with ease. By mastering looping statements, you can write more efficient and concise code and become a more effective programmer.



Looping statements are an essential component of any programming language. They allow programmers to repeat a specific set of instructions multiple times, saving time and reducing the amount of code required. JavaScript is no exception, and it offers several types of looping statements to help developers efficiently execute repetitive tasks. In this post, we will explore the different types of looping statements available in JavaScript and how to use them effectively.

So the basic use of Looping statements is, to repeat any block of code or task multiple times by using Loops. It will help developers to reduce the lines of code and rewriting work.

There are main 3 types of Loops available in Javascript :

  1. for loop
  2. while loop
  3. do-while loop
Every loop has its own mechanism, we can able to use any loop from this to do specific activities, but the developer has to pass correct statements in it.

1. for loop :

For loop is a basic and commonly used looping statement in Javascript.
This loop is used to iterate any block of code for a given limited time. Developers are passing the limit for the loop to iterate the block of code. It is ideal for iterating through arrays or performing a set of instructions a fixed number of times.

Syntax :
for (initialization; condition; increment/decrement)
{
  // code to be executed
}

Example :
for (let i = 1; i <= 20; i++)
{
  document.write(i+"<br>");
}


This example prints the Numbers from 1 to 20. To print the numbers, we have used a Variable ' i '.

Another Example :
Suppose You have an Array, which can hold the Name of Fruits. You have to display all the fruits present in the array, then You can able to do it manually by using their indexes. Like :
<p id="demo"></p>

<script>
    fruits = ["Apple", "Banana", "Custured Apple", "Mango", "Strawbery"];

    let text = "";
    text += fruits[0] + "<br>";
    text += fruits[1] + "<br>";
    text += fruits[2] + "<br>";
    text += fruits[3] + "<br>";
    text += fruits[4] + "<br>";

    document.getElementById("demo").innerHTML = text;
</script>

Here we have displayed the elements of Frtuits manually, but to avoid the length of code and rewriting work, the developers can use loops,

<p id="demo"></p>

<script>
    fruits = ["Apple", "Banana", "Custured-Apple", "Mango", "Strawbery"];

    let text = "";
    for(let i = 0; i < fruits.length; i++)
    {
        text += fruits[i] + "<br>";
    }

    document.getElementById("demo").innerHTML = text;
</script>


2. while loop :

The while loop is another type of looping statement available in Javascript.
It can iterate a block of code or set of instructions as long as the given condition is true. When the condition gets broken, then it stops the iteration and breaks the loop.
Basically, developers have to pass the conditions in while block to iterate a set of instructions. Then as per the condition, the while loop will iterate the block of code until the given condition will never break.
The condition is evaluated at the beginning of each iteration, and if it is true, the code inside the loop is executed.

Syntax :
while (condition)
{
  // code to be executed
}

Example :
let i = 1;
while (i <= 10)
{
  document.write(i+"<br>");
  i++;
}


This example prints the Numbers between 1 to 10. To print the numbers, we have used a Variable ' i '.

3. Do while loop :

The do...while loop is similar to the while loop, but it ensures that the code inside the loop is executed at least once, even if the condition is false.
It means, this loop will execute the block of code at least once, either condition is true or false.
After the 1st execution of 1st iteration, it will check the condition. If the condition is true, then it will execute 2nd iteration, then again checks the condition. It will continue this cycle until the condition gets false.

Syntax :
do
{
  // code to be executed
}
while (condition);

Example :
<p id="demo"></p>

<script>
    let input;
    let text = "";
    do
    {
        input = prompt('Enter a number:');
    }
    while (isNaN(input));
    
    text += "Valid number entered : "+input;

    document.getElementById("demo").innerHTML = text;
</script>


This example shows the working of do while loop.
In that, we have created a variable 'input', which can take the values from the user and passes this value in the while condition. If the passed value is a Number, then it will show "Valid number entered: +number ", but when the user passes any other character or string in the condition then, it will iterate the block of code, which will again prompt the user to enter a value until the user enters a numerical value in it.

So these are the 3 basic types of looping statements available in Javascript.

I hope it will help you to learn Javascript More...

Thank You..!!




Also Visit


Post a Comment

0 Comments