Posted By:
26 Aug, 2014 11:59 am PDT

Loops in JavaScript

I want to repeat a certain task 100 times and I don't think it tis worth writing same thing 100 times. Please help me to find a shorter way out.

I have heard of the loops.

Please provide me with the information about how do we use loops.

Attachment:None


Loops

While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need to write loop statements to reduce the number of lines.

JavaScript supports all the necessary loops to help you on all steps of programming.

The while Loop

The most basic loop in JavaScript is the while loop which would be discussed in this tutorial.

Syntax:

while (expression){

   Statement(s) to be executed if expression is true

}

 

The purpose of a while loop is to execute a statement or code block repeatedly as long asexpression is true. Once expression becomes false, the loop will be exited.

Example:

Following example illustrates a basic while loop:

<script type="text/javascript">

<!--

var count = 0;

  1. ("Starting Loop" + "<br />");

while (count < 10){

  document.write("Current Count : " + count + "<br />");

  count++;

}

  1. ("Loop stopped!");

//-->

</script>

 

The do...while Loop:

The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

Syntax:

do{

   Statement(s) to be executed;

} while (expression);

 

Note the semicolon used at the end of the do...while loop.

Example:

Let us write above example in terms of do...while loop.

<script type="text/javascript">

<!--

var count = 0;

  1. ("Starting Loop" + "<br />");

do{

  document.write("Current Count : " + count + "<br />");

  count++;

}while (count < 0);

  1. ("Loop stopped!");

//-->

</script>

 

The for Loop

The for loop is the most compact form of looping and includes the following three important parts:

  • The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.
  • The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.
  • The iteration statement where you can increase or decrease your counter.

You can put all the three parts in a single line separated by a semicolon.

for (initialization; test condition; iteration statement){

     Statement(s) to be executed if test condition is true

}

 

Example:

Following example illustrates a basic for loop:

<script type="text/javascript">

<!--

var count;

  1. ("Starting Loop" + "<br />");

for(count = 0; count < 10; count++){

  document.write("Current Count : " + count );

  document.write("<br />");

}

  1. ("Loop stopped!");

//-->

</script>

 
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud