spacer

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

home / programming / javascript / diaries / 5

[previous] [next]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

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

Loops

Loops are used to repeat a process or section of code a specified number of times. There are three types of loop statements: for, while, and do while. These statements use the increment/decrement and comparison operators we learned about previously.

The for Statement

A for statement is useful because it allows for the declaration of the variable, the condition for the statement, and the incremental expression all in one. The format of a for statement is:

  for (initialExpression; condition; incrementExpression) {
    statements
  }
  • The first portion declares the type of statement (e.g., for).
  • The next portion is the expression that is going to be used. It is declared as a variable.
  • Next, the condition that will be tested against the initial expression is given.
  • Finally, the incremental operator is given.

Here is a sample for statement. Be sure to try this yourself.

  for (var newNum=1; newNum < 4; newNum++) {
    document.write("This pass is #" + newNum + "<br>");
  }

Let's take a look at what's happening here.

  • The reserved word for begins the code for the loop.
  • Next, we declare the variable newNum and give it a value of 1
  • The first time the loop is executed, it will compare the initial value of the variable newNum with the conditional statement (newNum < 4). (This conditional statement is like an if statement: "If the variable newNum is less than four ....") If the conditional statement is false, the loop will not be executed. If the statement is true, the value will be used in the document.write statement. The value of the variable is not increased until after the loop executes once.
  • Next, the script will increase the value of the variable newNum by 1. It then executes the condition statement that we set-up which says: "As long as the value of the variable newNum is less than 4, perform the document.write statement." It will do this until the value of the variable newNum equals 4.
  • Once the value of the variable newNum equals 4, the loop will stop execution.

The ++ increment operator is used to increase the value of the variable by one. You can also use the decrement operator, --:

  for (var newNum=9; newNum>4; newNum--) {
    document.write("This pass is #" + newNum + "
"); }

If you needed to increase/decrease the value of the variable by a different number, say 3, you would write it as: newNum+=3 or newNum-=3, respectively.

home / programming / javascript / diaries / 5

[previous] [next]

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
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
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: