Tuesday, December 7, 2010

Loops
Often when you write code, you want the same block of code to run over and over again in a row.


In JavaScript, there are two different kind of loops:


while - loops through a block of code while a specified condition is true

for - loops through a block of code a specified number of times


The while Loop

The while loop loops through a block of code while a specified condition is true.


Example of while loop:-

<script type="text/javascript">
var i=0;
while (i<=5) { document.write("The number is " + i);
document.write("<br />");
i++;
}

</script>

Explanation:

i is equal to 0.

While i is less than , or equal to, 5, the loop will continue to run.

i will increase by 1 each time the loop runs.

try the loop.

No comments: