JavaScript

JavaScript: While Loops

JavaScript: While Loops

There are situations while programming when there is a need to perform a certain action again and again or, let's say, in a continuous loop. To execute such actions, you must write the code repeatedly until you achieve the number for which you want to perform the loop. The trouble of writing the same JavaScript code can be eliminated by using loops. 

Following are the loops supposed by JavaScript:

  1. while Loop
  2. do…while Loop

while Loop

You can use while loops to execute statements that are repeated continuously in JavaScript code. They are basic loops that carry out the execution of the statements defined under it or the code block written under it, the execution process carries out in a loop until the expression values out to be false. In this condition, the execution process finally terminates. 

Following is the syntax used for while loop  in JavaScript code:

while (expression needed to be executed) {
   The Statement that has to be executed if the above expression holds the value true.
}

Following is an example of a JavaScript code using above mentioned while loop:

<html>
   <body>
      
      <script type = "text/javascript">
         <!--
            var attempts = 5;
            document.write("Number of attempts");
         
            while (count < 7) {
               document.write("Present attempts : " + attempts + "<br />");
               attempts++;
            }
            document.write("Loop ended");
         //-->
      </script>
   </body>
</html>

The output for the program is:

Number of attempts

Present attempts: 5

Present attempts: 6

Loop ended

do...while Loop

Unlike the while loop, in the do…while loop, the checking for the required condition takes place at the end of the loop. As the checking for any matching statements takes place at the end of the loop, this concludes that the whole loop will be executed before any checking happens, concluding that the execution takes place at least once for the entire loop, also in case when the condition values out to be false. 

Following is the syntax used for doing…while loop  in JavaScript code:

do {
   The Statement that has to be executed if the above expression holds the value true;
} while (expression needed to be executed);

Always remember to use semicolons at the end of the statements after defining them. 

Following is an example of a JavaScript code using above mentioned do…while loop:

<html>
   <body>   
      <script type = "text/javascript">
         <!--
            var attempts = 2
            document.write("Number of attempts” + "<br />");
            do {
               document.write("Present attempts : " + attempts + "<br />");
               count++;
            }
            while (attempts < 8);
            document.write ("Loop ended");
         //-->
      </script>      
   </body>
</html>

The output for the program is:

Number of attempts

Present attempts: 2 

Present attempts: 3 

Present attempts: 4 

Present attempts: 5 

Present attempts: 6

Present attempts: 7

Loop ended