JavaScript tutorial-loops


JavaScript loops

Put simply, loop enables your program to execute the block of code repeatedly.

-For loop

syntax: for(start value;condition;increment){ statements;}

The code in for loop will be executed from the start value until the condition is met. The increment will be added continuously to the start value until the condition is met.

Example:
   function forLoop()
   {
      var i;
      for (i = 1; i <=10; i++){
         document.write("Cambodia");//The word Cambodia will be printed 10 times
   }
}

-While loop

The code in while loop will be executed at the time when the condition is true.

             Syntax:

             start value;

 while (condition){

 statement1;

statement2;

………..

increment;

                            }

Example:

function whileLoop()  {
               var i = 1;
               while (i <= 5)
             {
                document.write("JavaScript is simple to learn.");
                i++;
      }
   }



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.