Chapter 4. Loops and Debugging

In This Chapter

  • Creating for loops

  • Learning for loop variations

  • Building flexible while loops

  • Making well-behaved while loops

  • Recognizing troublesome loops

  • Catching crashes with debugging tools

  • Catching logic errors

  • Using the Aptana line-by-line debugger

  • Using the Firebug debugger

  • Watching variables and conditions

Computer programs can do repetitive tasks easily. This is accomplished through a series of constructs called loops. A loop is a structure that allows you to repeat a chunk of code. In this chapter you learn 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 very difficult to diagnose. But don't worry. I demonstrate a number of very powerful techniques for looking through your code to find out what's going on.

Building Counting Loops with for

One very standard type of loop is the for loop. You use these loops when you want to repeat code a certain number of times. Figure 4-1 shows a for loop in action:

It looks like ten different alert() statements, but there's only one. It just got repeated ten times.

This loop repeats ten times before it stops.

Figure 4.1. This loop repeats ten times before it stops.

Note

In Figure 4-1, and some of the other looping demos in this chapter, I show the first few dialog boxes and the last. You should be able to get the idea. Be sure to look at the actual program ...

Get JavaScript® and AJAX for Dummies® 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.