JavaScript looping statements with examples - Javascript

JavaScript looping statements with examples

JavaScript Looping Statements: Examples and Applications

In the world of programming, repetition is a common necessity. Whether it’s iterating through arrays, processing data, or executing tasks until a condition is met, looping statements play a crucial role in automating tasks and streamlining code. In this article, we’ll dive deep into JavaScript looping statements with examples, exploring their syntax, examples, and practical applications in web development.

Understanding JavaScript Looping Statements:

JavaScript offers several types of looping statements, each tailored to specific use cases:

  1. for Loop: The for loop is one of the most commonly used looping statements in JavaScript. It iterates over a block of code a specified number of times, with each iteration controlled by a loop counter or index variable.

Syntax-

            for(initialization;condition;increment/decrement)

             {

              Statements

              }

Example-1 WAP to print welcome msg 10 times.

<html>
<script>
for (i=1;i<=5;i++ )//i=i+1=2+1=3+1=4+1=5+1=6
{
document.write(“<br>Welcome in javascript loop statement…”);
}

</script>
</html>

Example-2 WAP to print 1 to 10 number

<html>
<script>

for (i=1;i<=20;i++ )
{
document.write(“<br>”+i);
}

</script>
</html>

Example-3 WAP to print odd numbers between 1 to 20 number

<html>
<script>
for (i=1;i<=20;i++ )//i=1+2=3+2=5…21
{
if (i%2!=0)
{
document.write(“<br>”+i);
}
}
</script>
</html>
Example-4 WAP to print even and odd numbers between 1 to 30 number
<html>
<script>
for (i=1;i<=30;i++)
{
if (i%2==0)
{
document.write(“<br>Even=”+i);
}
else
{
document.write(“<br>Odd=”+i);
}
}
</script>
</html>
Example-5  WAP to print table of any number.
<html>
<script>
var n=parseInt(prompt(“Enter any number…”))
for (i=1;i<=10;i++ )
{
table=n*i;
document.write(“<br>”+n+”*”+i+”=”+table);
}
</script>
</html>
Example-6 WAP to print number and its square and cube value up to 10.
<html>
<script>
for (i=1;i<=10;i++ )
{
document.write(“<br>Number=”+i);
document.write(“<br>Square=”+i*i);
document.write(“<br>Cube=”+i*i*i);
}
</script>
</html>
For Live Class Watch Our Video on YouTube by click on the below link
  1. while Loop: The while loop executes a block of code as long as a specified condition is true. Unlike the for loop, which requires an explicit initialization and incrementing of a loop counter, the while loop relies solely on the condition to determine when to terminate.

Syntax-

               Initialization;

            while (<condition>)

              {

                        Statement;

                        Increment/decrement;

              }

Example-1 WAP to print 1 to 10 number.

<html>
<script>
i=1;
while(i<=10)
{
document.write(i+”<br>”);
i++;
}
</script>
</html>
 

Example-2 WAP to print odd numbers 1 to 20 number

<html>
<script>
i=1;
while (i<=20)
{
document.write(i+”<br>”);
i=i+2;
}
</script>
</html>
Example-3 WAP to print table of any number
<html>
<script>
var num=parseInt(prompt(“Enter any number”))
i=1;
while (i<=10)
{
t=num*i;
document.write(t+”<br>”);
i++;
}
</script>
</html>
Example-4 WAP to find sum of the following series 1+2+3+4+5+….10=55
<html>
<script>
var sum=0;//1//3//6//10//15//21//28//36//45//55
i=1;
while (i<=10)
{
sum=sum+i;//6+4=10+5=15+6=21+7=28+8=36+9=45+10=55
i++;
}
document.write(“Sum of the series=”+sum);
</script>
</html>
Example-5 WAP to find sum of the following series 1+3+5+9….19=100
<html>
<script>
var sum=0;
i=1;
while (i<=19)
{
sum=sum+i;
i=i+2;
}
document.write(“Sum of the series=”+sum);
</script>
</html>
For Live Class watch our full video on YouTube by click on the below link
  1. do while Loop: Similar to the while loop, the do…while loop executes a block of code as long as a specified condition is true. However, the do…while loop guarantees that the block of code is executed at least once before checking the condition for subsequent iterations.

Syntax-

                initialization;

               do

               {

               Statement;

               } while (condition);

Example-1 WAP to print odd numbers 1 to 20 number

<html>
<script>
i=1;
do
{
document.write(i+”<br>”);
i=i+2;
}while (i<=20);
</script>
</html>

Example-2 WAP to find sum of the following series 1+4+9+16….100=385

<html>
<script>
var sum=0;//1//5//14
i=1;
do
{
sum=sum+i*i;//0+1=1+4=5+9=14
i++;
}while (i<=10);
document.write(“Sum of the series=”+sum);
</script>
</html>

Example-2 WAP to print number and its square value depending on user range.

<html>
<script>
var start=parseInt(prompt(“Enter start range…”))
var end=parseInt(prompt(“Enter ending range…”))
var i=start;
do
{
document.write(“Number=”+i+”<br>”);
document.write(“Square value=”+i*i+”<br>”);
i++;
}while(i<=end);
</script>
</html>

Practical Applications of JavaScript Looping Statements:

JavaScript looping statements find wide-ranging applications in web development:

  1. Iterating Through Arrays: Looping statements are commonly used to iterate through arrays, allowing developers to access and process each element individually.
  2. Generating HTML Elements: Looping statements can be used to dynamically generate HTML elements based on data from arrays or other sources, enabling the creation of dynamic web content.
  3. Handling User Input: Looping statements are useful for validating and processing user input from forms, ensuring that data is handled appropriately and errors are caught.
  4. Animating Elements: By utilizing looping statements in conjunction with CSS transitions or JavaScript animation libraries, developers can create dynamic and interactive animations on web pages.

Best Practices for Using JavaScript Looping Statements:

To maximize the effectiveness and efficiency of JavaScript looping statements, consider the following best practices:

  1. Use the Appropriate Looping Statement: Choose the looping statement that best fits the specific requirements and conditions of your task.
  2. Avoid Infinite Loops: Ensure that your looping statements have a clear termination condition to prevent infinite loops, which can cause the browser to become unresponsive.
  3. Optimize Performance: Minimize unnecessary computations and operations within looping statements to improve code performance, especially when dealing with large datasets or complex algorithms.
  4. Keep Code Readable: Use meaningful variable names and indentation to make your looping statements more readable and understandable for yourself and other developers.

Conclusion:

JavaScript looping statements are indispensable tools for automating repetitive tasks and iterating through data in web development. By mastering the syntax, examples, and practical applications of for, while, and do…while loops, developers can write more efficient, maintainable, and scalable code for a wide range of projects. Whether you’re a novice programmer or a seasoned developer, understanding and utilizing JavaScript looping statements effectively will undoubtedly enhance your coding skills and productivity in the dynamic world of web development.

Also Check our latest upload

JavaScript operators with example

JavaScript programs examples with output

1 thought on “JavaScript looping statements with examples”

Leave a Comment