Contents

Switch Breaks

Script Loop

Loops are used to execute a block of code a number of times.


A for loop runs from a start value to an end value. It's usually used to loop through every value in an array.


for (Start;Condition;Increment){
}

Example:


var Text="7 times table.\r\n";
for (var i=1;i<=12;i++){
Text
+=i+" x 7 = "+(i*7)+"\r\n";
}
alert(Text);

A while loop is used when you don't know how many times the loop should run. It continues only if a condition is met.


while(Value){
}

Example:


var i=1;
var Text="7 times table.\r\n";
while(i<=12){
Text
+=i+" x 7 = "+(i*7)+"\r\n";
i
++;
}
alert(Text);

A do...while loop is used when you don't know how many times the loop should run. It continues only if a condition is met. Unlike a while loop, it runs once before any conditions are tested.


do{
}while(Value);

Example:


var i=1;
var Text="7 times table.\r\n";
do{
Text
+=i+" x 7 = "+(i*7)+"\r\n";
i
++;
}while(i<=12);
alert(Text);

Switch Breaks