O'Reilly logo

HTML, XHTML, & CSS All-in-One For Dummies®, 2nd Edition by Andy Harris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. Loops and Debugging

In This Chapter

  • Working with for loops

  • Building while loops

  • Recognizing troublesome loops

  • Catching crashes and logic errors

  • Using the Aptana line-by-line debugger

Computer programs can do repetitive tasks easily, thanks to a series of constructs called loops. In this chapter, you discover the two major techniques for managing loops.

Loops are powerful, but they can be dangerous. It's possible to create loops that act improperly, and these problems are difficult to diagnose. I demonstrate several powerful techniques for identifying issues in your code.

Building Counting Loops with for

A loop is a structure that allows you to repeat a chunk of code. One standard type of loop — the for loop — repeats a chunk of code a certain number of times. Figure 3-1 shows a for loop in action.

This loop repeats ten times before it stops

Figure 3.1. This loop repeats ten times before it stops

Although it looks like ten different alert statements appear, only one exists; it's just repeated ten times.

Note

I show the first few dialogs and the last. You should be able to get the idea. Be sure to look at the actual program on the CD-ROM to see how it really works.

Building a standard for loop

You can see the structure of the for loop in the following code:

<script type = "text/javascript">
      //<![CDATA[
      //from BasicFor.html
      for (lap = 1; lap <= 10; lap++){
        alert("now on lap: " + lap + ".");
      } // end for
      //]]>
    </script>

for loops are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required