Loops

In programming writing the same codes over and over again can be very tedious and time consuming. In JavaScript if you want to repeat a block of codes several times you can use one of the Loops. Let us look at different types of Loops.


For Loop

The For loop is used if you know how many times you want your codes to be repeated.


Syntax

for (var= start(initial value); condition; increment++/decrement--)
{
statment to be executed
}


If a condition inside the for loop is true then the statement inside the curly bracket is executed. The increment/decrement counter will continue to increase/decrease by 1 until the condition is no longer true.



Example

<script>
var num = 4
for (var i=0; i>num; i++)
{
document.write( "num" + i");
document.write( "<br><br>");
}
</script>

Displays



While loop

In the while loop if the condition is true the statment(s) inside the curly bracket continues to be executed until the condition is false. The increment/decrement counter is after the statement. You can create an infinitive loop with the syntax while(true).


Syntax

while (condition)
{
Statement(s) is executed if condition is true;
}
increment++/decrement--


Example 1

<script>
var x=0;
while(x<=3)
{
document.write(" This x is number" + x);
document.write("<br><br>");
x++;
}
</script>

Displays



Example 2


<script>

var i=1;
var num=2;
while (i<=12)
{
document.write(i +"x" +num +"=" + (i*num))
i++;
document.write("<br><br>");
}

</script>

Displays

Do While Loop

In the do while loop the condition is at the end, so it will be executed at least once. Whereas, in the while loop the statment may never be excuted if the condition remains false.


Syntax

do
{
Statement(s) to be executed;
}
while (condition);


Example

<script>
var x=1;
do
{
document.write("Time is "+ x+" o'clock");
document.write("<br><br>");
}
while(x<=4);
</script>

Displays




For in Loop

The 'for in loop' is used when you want to loop through the properties and methods of an object.


Syntax


for (variablename in object)
{
statement to be executed
}


Example

<script>
var flavours;
var cake = new Array("lemon","chocolate","orange");
for (flavours in cake)
{
document.write(" This cake flavour is " + cake[flavours]);
document.write(" - deliciously tempting cake!");
document.write("<br><br>");
}
</script>


Displays






Break and Continue Statements

You have already seen the break statment in the switch loop. If you want to exit the loop at anytime you can do so by using the break statement.


Example:Break Statement

<script>
var time=1;

while(time<=12)
{
if(time==7)

{break;}

document.write( time +". I must wake up!");
document.write("<br><br>");
time++;
}
</script>

Displays




The continue statement breaks where you want it to and then continues on to the end of the loop.


Example:Continue Statement

<script>
var num;
for(num=1; num<=4; num++)
{
if ((num==2)||(num==3))
{continue;}
document.write("This is num " + num);
document.write("<br><br>");
}
</script>

Displays





Now do the Exercise!