IPOWERWEB.com

 Affordable, reliable
web hosting solutions

Call IPOWERWEB Today at 1-888-511-HOST Chat with an IPOWERWEB representative LIVE!
24/7x365 service - Live Technical Support

Domain Name Registration
web hosting services
cheap web hosting
IPOWERWEB help section
contact IPOWERWEB
testimonials for best hosting
affordable web hosting
IPOWERWEB web hosting
IPOWERWEB accolades
best domain prices


Web Hosting Money Back Guarantee
home build profit promote manage


Javascript Tutorial: Loops and Functions

Home > Build > Programming > Javascript > Javascript Tutorial

by Boris Mordkovich

Loops are pretty much what you would guess; they are used to repeat certain statements for a specified number of times. Loops can make the length of script decrease tremendously in size, thus saving you lots of time and effort.

THE for LOOP

For loops are used to repeat certain statements a specified number of times using a counter. Usually this counter is a number and has one added to it or taken away from it each time the loop executes. Time for an example.....
     for (i = 1; i <= 5; i++){
     
     }


Many people use the i variable as a counter. If you look at the scripts of many people you will often notice this. It is a very old programming tradition that started with a very old programming language called Forth.

Let's analyze what this script actually does. The loop is started by the for keyword and then it establishes a variable (the counter), the value to test the variable with, and how the variable is to be changed each time the loop executes and it also contains braces around the statements that are to be executed. In simpler terms, this script executes 5 times with value of i having one added to eat each time the loop executes. You use the same operators you used in the last chapter to compare values.

The result of the script would be 5 messages with a line break after each. It would appear like the following.
     Test message #1
     Test message #2
     Test message #3
     Test message #4
     Test message #5

THE while LOOP

While loops are usually used when you don't have a counter.
     var i = 1;
     while (i <= 5){
     
     }

This would achieve the same affect as the for loop above. Like I mentioned, you don't have a counter with while loops.

THE do...while LOOP

     do {
     
     }
     while (i <=5);

do...while loops are a lot like while
loops but they check the variables at the end of the loop instead of the beginning. This means that the loop will execute at least once.

 
home | products | about us | help center | testimonials | press room | contact us
affiliates | careers | domain names | web hosting | site map

Copyright © 1999-2007 IPOWER, Inc. Read our Terms and Conditions. All rights reserved.