The "while" loop evaluates an expression, and while the expression evaluates to "true", the "while" loop will repeatedly execute a series of statements. The difference between the "while" loop and the "do/while" loop is that the "do/while" loop executes the statements inside the curly brackets one time first before it evaluates the "condition" expression.
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

SurveyClub

The while 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 while loop evaluates an expression, and while the expression evaluates to true, the while loop will repeatedly execute a series of statements. The syntax for the while loop is shown below.

while(condition)
{
   statements;
}

The Condition expression inside the brackets is evaluated. The result of that evaluation can be true or false. If the result is true the statements inside the curly brackets will be executed. If the result is false, the loop terminates and program flow continues with the next statement following the while loop structure.

var price = 10.00;

while(price > 5.00)
{
   price -= 1.00;
}

alert("The final price is: " + price);

In the example above, the variable price is initialized to 10.00. The Condition expression inside the while loop brackets tests the value of price to see if it's greater than 5.00. If the value of price is greater than 5.00, the statements inside the curly brackets are executed; 1.00 is subtracted from price. The condition expression again tests the value of price.

After the statements inside the curly brackets are executed five times, the value of price is no longer greater than 5.00. The result of the condition expression evaluation becomes false, the while loop terminates.

program flow continues with the next statement following the while loop structure, which creates a message box displaying the final value of price (5.00).

The do/while Loop

A structure closely related to the while loop is the do/while loop. The syntax for the do/while loop is shown below.

do
{
   statements;

}
while(condition);

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