JavaScript Loops, JavaScript Tutorial, Global Guide Line Technology.
Bookmark Bookmark: JavaScript Loops, JavaScript Tutorial Forum | Blog |

      Home                    

Basic JavaScript Guide.
 JScript Introduction.            
 JScript Why?                       
 JScript Recognitions.          
 JScript onMouseover.         
 JScript with Buttons.           
 JScript Alerts.                     
 JScript Variables.               
 JScript Operators.               
 JScript Functions.               
 JScript Prompts.                 
 JScript Conditions.             
 JScript Validations.            
 JScript Confirmation Boxes.
 JScript Browser Detection.  
 JScript Redirection.            
 JScript on Links.                 
 JScript Switch.                    
 JScript Loops. 
 JScript Events.                    
 JScript Exception Handling.
 JScript Reserve Characters. 
 JScript with Objects.            
 JScript Math Object.           
 JScript using Dates.             
 JScript using String.            
 JScript using Arrays.            
 JScript Operations.              
 JScript Handling Cookies.   
 JScript in Mapping.             
 JScript Time Out.                
 JScript Summary.                

JavaScript Tutorial >> JavaScript For, Do and While Loops.


     Back             Next     


In any programming language there are a lot of available looping statements,  where we can execute our statements for one or more times. We can do this though the use of loops here in JavaScript. Now in JavaScript will guide us that the concept of looping is based on the iterations, and on each iteration some values are changed till the loop executes.  JavaScript loop is a circle, and this circle repeats itself till we want, and this activity when call or recall one time then its called one iteration.  The JavaScript loops are typically controlled by a test of a JavaScript variable, and it is that major variable which control the iterations of loop, because if we don't do nothing with the control of JavaScript Loop, it will be an infinite loop which does the same thing again and again, and an infinite loop never stops. That's not good in programming approach, and it may be the answer why our JavaScript hangs for a long time and it produces no result, the reason would be the infinity of JavaScript loop only...

In JavaScript there are four types of loops as below.
1 >> JavaScript for loop
2 >> JavaScript for...in loop
3 >> JavaScript while loop
4 >> JavaScript do...while loop
The easiest one is the JavaScript for statement. It specifies a counter JavaScript variable which will use for a test condition, and an action that updates the counter before each iteration.

Syntax of for loop in JavaScript

for (variable_initialization; expression; increment){
    JavaScript statements
}

On above "for" executes the JavaScript statements as long as the specified and expression is evaluated to "True" till the life of JavaScript loop. The following example fills an array with numbers from 0 to 6.

Example of JavaScript for loop

<script language="javascript">
var day = new Array();
for (i = 0; i < 7; i++) //as long as the variable "i" is smaller than 7
{
   day[i] = i;
}
</script>

Syntax of for in loop in JavaScript

JavaScript For---In loop is a special kind of loop structure in JavaScript. JavaScript uses it for stepping through all the elements of an array, or all the user-defined properties of an object. The syntax is a little different from the previous one

<script language="javascript">
for (variable in [object | array]){
  JavaScript statements
}
</script>

Now if we want a more open advance to JavaScript loops, we can always use the while structure. There is a slight difference between JavaScript for loop and JavaScript while loop, when we use JavaScript while, we must do all the counter JavaScript variable initialization and variation ourself. "while" will only accept a expression, and if the expression is evaluated to "True", then it will execute the JavaScript statements we provide. Because JavaScript while loops don't have an explicit built-in counter JavaScript variable, they are more exposed to JavaScript infinite loops, so always remember to change the counter-variable's value inside the JavaScript loop! Here is the same JavaScript loop example as before, but we use JavaScript "while" instead of JavaScript "for" here in JavaScript Tutorial.

Syntax of while loop in JavaScript

<script language="javascript">
var day = new Array();
i = 0; //we have to do the initialization ourselves here
while(i < 7)
{
  day[i] = i;
  i++;
  //don't forget to alter the counter-variable's value,
  //or else we'll get an infinite loop here
}
</script>

Similar to the JavaScript while loop is the JavaScript do...while loop, which actually performs the test after it executes the loop. This guarantees that the JavaScript loop is executed at least once, after which it may or may not continue, based on the evaluation of the expression provided with while loop.

Syntax of do while loop in JavaScript

<script language="javascript">
var numbers = new Array();
i = 0;
do
{
  numbers[i] = i; //this will parse at least once...
  i++; //this will also parse at least once
} while (i < 10);
</script>

There are times when we encounter special values within a JavaScript loop, and we may want to simple exit the loop, or continue to the next iteration of our loop. JavaScript provides two such statements one is  "break" and second is "continue". The JavaScript break statement immediately stops the execution of any loop, while the JavaScript continue statement stop the current iteration only, and starts the next one. No matter which statement we choose to use, the previous statements before JavaScript break statement or JavaScript continue statement are always executed, but the below ones will not be parsed.

We can use JavaScript break statement and JavaScript continue statement with any of the JavaScript "for", "while" or "do...while" loops.

Syntax of breaking any loop in JavaScript

<script language="javascript">
var day = new Array();
for (i = 0; i < 7; i++) //as long as the variable "i" is smaller than 7...
{
  if(i == 4)
    continue; //let's skip the number 5
    numbers[i] = i;
}
</script>

     Back             Next     
 

JavaScript For, For In, Do, and Do While Loops Examples



JavaScript If Else and Switch Statements Examples



[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2018 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 236 visitors are online now