spacer

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

home / programming / javascript / diaries / 5

[previous] [next]

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

Nested Loops

You can also nest for statements to obtain greater control and add a variety of tasks. Try this one and see what happens.

  for (var jsNot=0;jsNot<10;jsNot++) {
    document.write("JavaScript is not Java!<br>");
      for (jsNot2=1;jsNot2<2;jsNot2++) {
        document.write("Are you getting this?<br>");
      }
  }

It's a little tedious but you get the picture. Let's take a look at what is happening here:

  1. In the first for statement, the variable jsNot is declared and given a value of 0.
  2. It's then compared with the conditional statement to see if it's less than 10. If the conditional statement is true, the script will print: "JavaScript is not Java!" to the screen. If the conditional statement is false, the loop will end.
  3. If the conditional statement is true, the value of jsNot is increased by 1. The nested loop then begins.
  4. The variable jsNot2 is declared and given a value of 1.
  5. It's then compared with the conditional statement to see if it's less than 2. If the conditional statement is true, the script will print: "Are you getting this?" to the screen. If the conditional statement is false, the loop will end and control will return to the main loop.
  6. If the conditional statement is true, the value of jsNot2 is increased by 1. Since the variable's value is no longer less than 2, the loop ends and control returns to the main loop where the entire process begins again. It's the counter in the main loop that determines when the entire looping process ends.

You can also nest other types of blocks. Let's take a previous script and add an if nesting loop to it:

  for (var newNum=1; newNum<4; newNum++) {
    document.write("This pass is #" + newNum + "<br>");
     if (newNum==3) {
       document.write("That's all she wrote folks!");
     }
  }

In this loop, when the variable newNum equals 3, which would be on its last loop, an if statement is executed. Let's try another one that's a little more complex:

for (var jsNot=0;jsNot<10;jsNot++) {
  if (jsNot==4) {
    document.write("Are you getting this?<br>");
  }
  else if (jsNot==9) {
    document.write("I think you got the point!");
  }
  else {
    document.write("JavaScript is not Java!<br>");
  }
}

The continue and break Commands

You can also control loops using the continue and break commands. These allow you to skip a certain part of the loop, or stop it altogether, if a condition is met. Look at the following script:

  for (var newNum=1; newNum<9; newNum++) {
     if (newNum==5) {
       continue;
     }
    document.write("This pass is #" + newNum + "<br>");
  }
  • The first portion of the loop is what we have seen before. Once the variable is declared, initialized, and the conditional statement is found to be true, the loop moves to the if statement.
  • The if statement says that if the variable newNum is equal to 5, skip the rest of the loop and start at the next increment; otherwise, the remainder of the code for the loop will be executed.

A break command acts a little differently in that it actually stops the loop from executing any additional code within the loop:

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

In this loop, if the variable newNum is equal to 5, the loop will stop execution completely.

I'm sure you can see how this could be beneficial in many cases. Try them yourself and see what happens.

Nesting loops add another dimension to your programming skills that aid you in creating more effective scripts. Don't be afraid to use them.

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
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: