spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 5

[previous]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 5

The while statement

A while statement is good for short comparisons. It will continue to repeat until the comparison is false. Unlike the for statement, it does not create its own variable. It must use an existing one from the main script. Its format is:

while (condition) {
   statements
}

With this type of loop it's important to remember that the initial value of the variable must be declared before the loop and the value must be adjusted within the loop, otherwise it will repeat indefinitely. We can use the while statement to do what we did with the for statement previously:

var jsNot=1;
while (jsNot<11) {
  document.write(jsNot + ".JavaScript is not Java!<br>");
  jsNot+=1;
}

Notice that the variable is declared for the loop prior to its declaration; the comparison statement is located after the while declaration; and the incremental statement is after the performed action. Without the incremental statement, the script won't stop, it will go on indefinitely. (I know, I tried it. Thanks to Firefox, it detected it and asked me if I wanted to stop it. Nice feature!)

The diagram below may be helpful.

Comparison of the for and while statements

The do while statement

This is different from the while statement in that it will always execute the loop at least once, even if it's false. The comparison is made at the end of the statement. The format is:

do {
   statement
} while (condition);

An example would be:

var i=1
do {
  document.write(i + "<br>")
  i++
} while (i <= 6);

In this statement, we first declared a variable named "i" and initialized it with a value of 1. (The variable "i" is generally used as a variable name for this kind of a counter.) Next, the statement will print out the number 1 to the Web page using the document.write statement. Then the value of "i" will be increased by 1 using the ++ increment operator. The last line, the while portion of the statement, checks to see if the variable "i" is less than or equal to (<=) the number 6. If the statement is true (the variable "i" is less than or equal to (<=) the number 6), then the loop will start over again. If the statement is false, the loop will end.

Wrap-Up

That covers our study of conditional statements and loops. Hopefully you have gained enough insight to start creating some of your own scripts, albeit very simple ones. Perhaps you can even modify some existing ones to better suit your needs. Whatever the case, keep on scripting! See you next time.

To Part 6 Continue on to "The JavaScript Diaries: Part 6"

home / programming / javascript / diaries / 5

[previous]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: June 10, 2005

URL: