A loop structure allows you execute a series of statements over and over. Several kinds of loop structures are available in Java Script, the most common loop is the for loop, which enables you to execute the loop a specific number of times.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

The for Loop

A loop structure allows you execute a series of statements over and over. Several kinds of loop structures are available in Java Script, the most common loop is the for loop, which enables you to execute the loop a specific number of times.

The control statement of the loop has three sections, the initialization expression, the comparison test and the update expression as shown below.

for(initialization; comparison; update)
{
   statements;
}

The initialization expression is where you perform any initialization required for the loop, usually you create and initialize the loops counter or index variable here.

The update expression is the third section of the for loop's statement. Here, you usually increment the loops counter or index, which tracks how many times the statements in the body of the loop have been executed.

The comparison test is where you evaluate or test the loops counter or index to determine if the statements in the body of the loop have been executed the required number of times, called iterations.

for (var i = 1; i <= 10; i++)
{
  document.write("This is iteration number " + i + "<br />");
}

The for loop shown above initializes the variable i to 1, increments i by 1 on each iteration of the loop, and performs the comparison test i <= 10 is i less-than or equal-to 10 to determine if the loop has executed 10 times. The statement in the body of the loop prints the value of the loop's control variable on each iteration as shown below.

This is iteration number 1
This is iteration number 2
This is iteration number 3
This is iteration number 4
This is iteration number 5
This is iteration number 6
This is iteration number 7
This is iteration number 8
This is iteration number 9
This is iteration number 10

The loop's control variable can be initialized to any number, incremented by any value and tested for any condition. The loop shown below initializes the variable j to 4, increments j by 2 on each iteration, and performs the test j <= 12 to determine if the loop has executed the required number of times.

for (var j = 4; j <= 12; j = j + 2)
{
  document.write("This is iteration number" + j + "<br />");
}

The statement in the body of the loop prints the value of the loop's control variable as shown below.

This is iteration number 4
This is iteration number 6
This is iteration number 8
This is iteration number 10
This is iteration number 12

The loop shown below initializes the variable k to 10, decrements k by 1 on each iteration, and performs the test j > 0 to determine if the loop has executed the required number of times.

for (var k = 10; k > 0; k--)
{
  document.write("The value of k is " + k + "<br />");
}

The statement in the body of the loop prints the value of the loop's control variable as shown below.

The value of k is 10
The value of k is 9
The value of k is 8
The value of k is 7
The value of k is 6
The value of k is 5
The value of k is 4
The value of k is 3
The value of k is 2
The value of k is 1

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269