JS -- Lesson 3

Lesson 3

In this lesson we will be talking about the "for" and "while" loops.
This lesson is a little tricky for any newcomers so pay attention as close as you can, and if you must, go over this lesson until you get it as best as you can.

To start us off lets go over the first part of the lesson we will be doing, "for" loops. "For" loops are used to continuously loop without you having to do a long tedious task, and will stop when you tell it to.

<SCRIPT LANGUAGE="Javascript">

for ( i = 1; i <= 10; i++ )
document.write("i is now equal to " + i + ".<br>")


That above is a for loop and will create the below result:

See how nifty it can be, we wrote something 10 times, while only really writing it once. There are many ways to use this, but youll have to figure out how to use it for yourself, i may help you out in the extras section of our site, but youll have to check it out occasionally.

Now to tell you what happened and how it happened.

First off we created the script language javascript tag, so we can write a javascript code, which is obvious. Next we made a for loop condition by writing for followed by 2 parantheses. Inbetween those parantheses is the condition of the loop, this is the most critical part in understanding loops. Just like if-else statements for loops have conditions but are a little different. In these conditions there are 3 factors, we must seperate these factors from one another with the semi-colon.
1. The first factor is the variable statement, this statement tells us which variable we are using. We made a brand new variable called i and made it equal 1. Now we will be working with a variable i.
2. The second factor is the actual condition of the loop, if this stays true the loop continues, once it is not true, false, the loop stops. In this case we keep writing i until i is greater in value than 10.
3. The third factor is what makes the loop keep looping. It tells javascript to (+1) to i every time it loops. Using the i++ is the same as doing i+1, but i++ seems more fancy to me, how bout you?

In final, we first make a variable i and set it to 1, then as long as it is less than or equal to 10 the loop will keep looping and everytime it loops we add 1 to i.

Doing all this will make i increase 1 in value everytime it loops, and using document.write we can write what i equals for each loop, understand?

While loops

While loops are more similar to if-else statements than are "for" loops, since while loops only have 1 factor condition.

<SCRIPT LANGUAGE="Javascript">

var loopy = 1;

while ( loopy <= 5 )
document.write("Now loopy is " + loopy + ".<br>")

document.write("Loopy loop is done!!")


The result is below.....

We create a variable called loopy and make it equal 1. Then we create the while loop, and make its condition so that it will keep looping until loopy is more than 5. In this case though we do the +1 to the variable within the loop. Its needed to stop the loop! After its done looping it will go to the next part of the code, and write that its done. While loops are much easier to understand than for loops but i like for loops better.

That is it for this lesson, check out the next one when you're ready.
Join Today
Join our Website's Jester Forum today!
Its FREE and easy......
Click Here


Enjoy the STAY

If you enjoyed this page,
add 2 favorites or tell a friend

Daily Event
Fortune of the Day

Quick Search
Ads & News
38450 visitors
=> Do you also want a homepage for free? Then click here! <=