JavaScript Resources
 
 
Categories
Misc

beginners php
programmershelp
programmingsite
software directory
Low Cost domains
Mobile phone business
Free stuff


 

Stores

 

 
Looping in JavaScript

These are times when the same portion of code needs to be executed many times with different values .

for

The for structure loops a preset number of times . The for loop is made up of two parts the condition and statements . The condition portion of the structure determines how many times the loop repeats while the statement is what is executed every time the loop occurs.

The conditional structure is contained within parentheses and is made up of three parts , each separated by a semi colon ( ; ) . The first part of the condition structure initializes a variable to a starting value . In most cases , the variable is declared here and initialized  . The second part is the actual conditional statement that determines how many times the loop with be iterated . The third and final part determines how the variable which was initialized in the first part , should be changed each time the loop is iterated . The third part causes the variable to be incremented , decremented etc . Here is the syntax of the for loop

for (initialize ; condition ; adjust)
{
    statement ;
}

You have to beware of infinite loops . Lets look at a for loop example .

<script language ="JavaScript">
<!--
for(var i = 0 ; i <= 10 ; i++)
{
	document.write(" " , i );
}
//-->
</script>

which gives us the following result

while

The while loop is similar to the for loop . The statement works like this while the expression in brackets is true execute the statements in the loop . Once the last statement is executed return to the top of the loop and evaluate the expression again . When the expression evaluates to false the next line of code after the while loop structure executes . To prevent infinite loops a statement must be included in the loop that modifies a variable in the expression . Here is the syntax of the while loop .

while (expression)
{
    statement;
}

Here is an example

<script language="JavaScript">
<!--
var i = 0;
while(i <= 10)
{
	document.write(" " , i);
	i++;
}
//-->
</script>

which gives us the following results

do while

This loop is simply a variant of the while loop . The important difference is that the loop will always evaluate once before evaluating the expression for the first time . Here is the syntax.

do
{
    statement ;
}
while (expression);

Once the loop has executed for the first time the expression in brackets is evaluated . If this is true the loop is executed once again . If this is false the next of line of code after the while structure is executed . A statement must be included within the loop that modifies a variable in the expression to prevent infinite loops .

Here is the example above modified to use a do ... while loop

<script language="JavaScript">
<!--
var i =0;
do
{
	document.write(" " , i);
	i++;
}
while(i <=10);	
//-->
</script>
and here is the result again

for .. in

This is not the same as the for loop . The for...in loop is used to provide access to the enumerated properties of a JavaScript object . This loop is only found in JavaScript . The statement in the loop are executed for each property of an object until every property has been accessed. The syntax looks like this

for ( variable in object)
{
    statement ;
}

Here is an example that accesses all the properties of the document object

<script language="JavaScript">
<!--
var i;
for(i in document)
{
document.write(" " ,i);
}
//-->
</script>

 

and here is the result

Break / Continue

Break is used to terminates execution of the innermost enclosing loop and from JavaScript 1.2 onwards you can also name the loop.

break ;
break label ;

Continue is restarts the innermost loop or from JavaScript 1.2 onwards can actually restart a named loop .

continue;
continue label;

 

 

SPONSORS

© 2003 onwards programmershelp