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

Get HTML, XHTML, & CSS All-in-One For Dummies®, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.