JavaScript Lecture          

Instructor: Amadou O. Wane
Last update: 10/09/01

Lecture5

For Loop
A repeat loop cycles though a group of statements until some condition is met. For instance, you can use a repeat loop to cycle though an array and print it to the screen. Or, you might want to cycle though the Array and look for some specific information using the if statement. The for loop is the most common used repeat loop. Here is the syntax for it.

 for ( [initial exp.]; [condition]; [update exp.] ){
    statements
    }

The square bracket means that the parameter is optional. I always use all three parameters and think you will too. The initial expression defines the starting point of the of the loop. It sets the value of a variable to some value. The condition tests the variable for a certain condition and when it is reached stops the loop and lets the code after the for loop execute. The update expression determines how the variable is incremented.

Here is a simple example. We will use the "for loop" to print the numbers 1 through 5 to our screen.

Here is code

 var i
 for(i=1; i<=5; i++){
   document.write(i + "<BR>")
   }

You can declare the variable as part of initial expression. In fact this is usually the way it is done. Here is our simple script with this change.

 for(var i=1; i<=5; i++){
   document.write(i + "<BR>")
   }

Sometimes you may want to break out of a loop or skip part of a loop. The break statement allows you to get out of a loop if some specific condition occurs. This is done by detecting the condition with an if statement that executes the break statement.

While loop & do-while loop

Two additional repeat loops are available in JavaScript. The while loop tests the supplied condition and continues to execute until it is met. Here is the syntax for it.

 while (condition){
    statements
    }
Example:
i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br>")
i++
}

Here is the syntax for the do-while loop.

 do{
    statements
    } while (condition)
Example:
i = 0
do
{
document.write("The number is " + i)
document.write("<br>")
i++
}
while (i <= 5)

The primary difference between these two loops is that the "do-while loop" will always execute one time whether the condition is true or not whereas the "while loop" will only execute if the condition is true.I could have used one of these loops to replace the "for loops" in our calendar. This would have eliminated the need for the break statement. However, I am not convinced that either of these loops have a lot to offer over the "for loop". Again, it is your choice.

Be aware that when you are using any of the loops that you can set up a condition that will cause a continuous loop if you are not careful with your coding. This will cause your page to hang up. So make sure that the condition that will stop execution of the loop is a realistic condition that will be met.

function my_addition (a,b)
{ var my_sum = a+b;
document.write("The sum of " +a+" and" + b + "is: " + my_sum);
}
//--------------------------------------------------------
var abc = prompt("Please enter a number:", "");
var my_add = 0;
the_input = parseInt(abc);
for (i = 1; i <= the_input; i++)
{ my_add = my_add + i;
document.write("<br>The result is: " + my_add);
}
//--------------------------------------------------------
j = 0
while (j <= 5)
{
document.write("The number is " + j)
document.write("<br>")
j++
}

//-------------------------------------------------------------
//FOR LOOP
//for ([initial expression]; [condition]; [update expression])
//{ }
/*answer = prompt("Enter an integer:","");
if (answer > 100)
{ alert("The number is greater than 100");
}
else
{
var i =0;
var j=0;
for (i=answer; i>=0; i--)
{ document.write("<br>The value of i : " + i);
}
}*/
//-------------------------------------------------------------
answer2 = prompt("Enter an integer:","");
if (answer2 > 100)
{ alert("The number is greater than 100");
}
else
{
var i =0;
var j=0;
var k = "";
for (i=answer2; i>=0; i--)
{
k = k + "*";
document.write("<br>" + k);
}
}
//-------------------------------------------------------------
for (i=0; i<5; i++)
{
var my_var = "my_var" + i;
my_var = prompt("Enter an integer:","");
document.write("<br>" + my_var);
}

//-------------------------------------------------------------
//WHILE LOOP
i = 0;
while (i <=5)
{ document.write("<br>The number is " + i);
i++
}

//--------------------------------------------------------