Loops

In this paragraph, we will learn something interesting called loop.

Consider that you need to print a line 100 times using JavaScript. What you will do?

You can type document.write("The line I want You to Write"); 100 times in your program or you can use loop.

The basic use of loop is to do something more than one time. Say, you need to print all the integers of 1 + 2 + 4 + 6 +…………+100 series upto 100. The calculation is the same, you only need to do it multiple times. In these cases, we use loop.

We will discuss two types of loops, namely for loop and while loop.

The for loop

The basic structure of the for loop is as follows:

for(starting ; condition ; increment/decrement)
{
  statement
}

The starting parameter is the initialization of your loop. You need to initialize the loop in order to start it. The condition parameter is the key element to control the loop. The increment/decrement parameter defines how your loop will increase/decrease.

Let's see an example. You want to print javascript is fun 10 times. The code will be as shown in the following:

<html>
  <head>
    <title>For Loop</title>
  </head>
  <body>
  <script type="text/javascript">
    var java; 
    for(java=0;java<10;java++){
      document.write("javascript is fun"+"<br>");
    }
  </script>
  </body>
</html>

The output will be similar to the following:

The for loop

Yes! You printed the line 10 times. If you look at the code carefully, you will see the following:

  • We declared a variable named java
  • In the for loop, we initialized 0 to its value
  • We added a java<10 condition that made the browser count from 0 to 10
  • We incremented the variable by 1; that's why we added java++

Exercise

  1. Write a code using JavaScript that will print the following output:
    I have 2 apples.
    I have 4 apples.
    I have 6 apples.
    I have 8 apples.
    I have 10 apples.
    I have 12 apples.
    I have 14 apples.
    I have 16 apples.
    I have 18 apples.
    I have 20 apples.
  2. Write a code that will print all the even numbers from 2 to 500.

The while loop

You have already have learned how to execute something multiple times using the for loop. Now, we will learn another loop known as the while loop. The structure of while loop is as follows:

initialize;
while(condition){
  statement; 
  increment/decrement; 
}

The codes for the previous example will be like the following:

<html>
  <head>
    <title>For Loop</title>
  </head>
  <body>
    <script type="text/javascript">
      var java = 0;
      while(java < 10){
        document.write("javascript is fun"+"<br>");
        java++;
      }
    </script>
  </body>
</html>

The output will be the same as the for loop.

Exercise

  1. Write a code that will print all the odd values from 1 to 600 using the while loop. (Hint: use the modulus operator.)
  2. Write a code that will print the following output:
    5 x 1  = 5
    5 x 2  = 10
    5 x 3  = 15
    5 x 4  = 20
    5 x 5  = 25
    5 x 6  = 30
    5 x 7  = 35
    5 x 8  = 40
    5 x 9  = 45
    5 x 10 = 50
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.123.120