JavaScript Basics :
LOOPS








Imagine that you wanted a script to perform the same routine over and over again 50 times in a row.

An example could be if you wanted a script to produce a table comparing temperatures in Fahrenheit and Celsius.
The script should produce 50 lines in a table showing different temperatures according to the two scales.

Instead of adding 50 almost equal lines in your script you could use loops to make the script perform a task like this.




There are two different kinds of loops: for and while.

The for loop is used when you know in advance how many times the script should perform.
For example if you wanted it to create exactly 50 lines.

The while loop is used when you want the loop to continue until a certain condition becomes true.
For example, if you wanted to make a table comparing Celsius and Fahrenheit, stepping 15 degrees for each row, and you wanted the table to contain values up to 1200 degrees of Celsius.

Below is a description of each of these two loops:




FOR LOOPS:

SYNTAX:
for (variable=startvalue; variable<=endvalue; variable=variable+incrementfactor)
{

// Here goes the script lines you want to loop.
}


Enter a variablename where it says variable.
Enter the startvalue of the loop where it says startvalue.
Enter the endvalue of the loop where it says endvalue.
Enter the factor each loop should increment where it says incrementfactor.

Note: The incrementfactor could be negative if you wanted.
Furthermore the <= could be any comparing statement, ex. >, == or whatever.


EXAMPLE:

<html>
<head>
<title>Celsius-Fahrenheit Converter</title>
</head>

<body>
<table border=3>
<tr><td>CELSIUS</td><td>FAHRENHEIT</td></tr>
<script language="javascript">
for (celsius=0; celsius<=50; celsius=celsius+1)
{
document.write("<tr><td>"+celsius+"</td><td>"
+((celsius*9/5)+32)+"</td></tr>");
}

</script>
</table>
</body>
</html>


Click here to see the page from this example.





WHILE LOOPS:

SYNTAX:
while (variable<=endvalue)
{

// Here goes the script lines you want to loop.
}



Enter a variablename where it says variable.
Enter the endvalue of the loop where it says endvalue.

Note: The <= could be anything that would fit the purpose ex. >, == or whatever.

EXAMPLE:

<html>
<head>
<title>Celsius-Fahrenheit converter</title>
</head>

<body>
<table border=3>
<tr><td>CELSIUS</td><td>FAHRENHEIT</td></tr>
<script language="javascript">
celsius=0;
while (celsius<=50)
{
document.write("<tr><td>"+celsius+
"</td><td>"+((celsius*9/5)+32)+"</td></tr>");
celsius=celsius+1;
}

</script>
</table>
</body>
</html>


Click here to see the page from this example.





BREAK & CONTINUE

Two special commands can be used in loops: break and continue.

break simply breaks the loop and continues with what might follow after the loop.

An example would be if you had a loop calculate the squareroot of numbers decrementing from 50.

Since calculating the square root of a negative number is an illegal mathemathic operation you would like the loop to end when the square root of zero had been calculated.

To do this you would add this inside your loop:

if (value==0) {break};






continue breaks the current loop and continues with the next value.

An example would be if you had a loop that divided a certain value with a factor of numbers ranging from -50 to +50.

Since division by zero is an illegal mathemathic procedure the loop would look like this:

for (value=-50; value<=50; value=value+1)
{
if (value==0) {continue};
document.write((100/value)+"<br>");
}


 << PREVIOUS
READ MORE >>  
















DEVELOPER TIP!





     "Better Than Books - As Easy As It Gets!"