HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [146]
♦ Build a sentry variable, using a sensible initial value. If you want the loop to start at 5, use 5 as the initial value.
♦ Check against a condition. It makes sense for a 5 loop to end at a multiple of 5. If you want this loop to continue until you get to 25, continue as long as i is less than or equal to 25.
♦ Modify the variable on each pass. In the example, the statement i += 5 adds 5 to i. (It’s just like saying i = i + 5.)
Fortunately, all these elements are in the for loop structure, so you probably won’t overlook them. Still, if you find that your loop isn’t working as expected, you may need to look into the debugging tricks described in the section “Catching Logic Errors” later in this chapter.
Looping for a while
The for loop is useful, but it has a cousin that’s even more handy, the while loop. A while loop isn’t tied to any particular number of repetitions. It simply repeats as long as its condition is true.
Creating a basic while loop
The basic while loop is deceptively simple to build. Here’s an example:
This script asks the user a simple math question and keeps asking until the user responds correctly. You can see it in action in Figure 3-4.
Figure 3-4: This loop continues until the user answers correctly.
The operation of a while loop is easy to understand. Here’s how the math program works:
1. Create a variable called answer to act as a sentry variable for the loop.
2. Initialize the variable.
The initial value of the variable is set to “-99”, which can’t possibly be correct. That guarantees that the loop will execute at least one time.
3. Evaluate the answer.
In this particular program, the correct answer is 5. If the value of answer is anything but 5, the loop continues. In this example, I’ve preset the value of answer to “-99” so that the loop happens at least once.
4. Ask the user a challenging math question.
Well, a math question, anyway. The important thing is to change the value of answer so that it’s possible to get 5 in the answer and exit the loop.
5. Give the user some feedback.
It’s probably good to let the user know how she did, so provide some sort of feedback.
Avoiding loop mistakes
A while loop seems simpler than a for loop, but while has exactly the same basic requirements:
♦ A critical sentry variable typically controls the loop. Some key variable usually (but not always) controls a while loop.
♦ The sentry must be initialized. If the loop is going to behave properly, the sentry variable must still be initialized properly. In most cases, you’ll want to guarantee that the loop happens at least one time.
♦ You must have a condition. Like the for loop, while loops are based on conditions. As long as the condition is true, the loop continues.
♦ You must include a mechanism for changing the sentry. Somewhere in the loop, you need to have a line that changes the value of the sentry. Be sure that it’s possible to make the condition false, or you’ll be in the loop forever!
If you forget one of these steps, the while loop may not work correctly. Making mistakes in your while loops is easy. Unfortunately, these mistakes don’t usually result in a crash. Instead, the loop may either refuse to run altogether or continue on indefinitely. If your loop has that problem, you’ll want to make sure that you read the next section.
Introducing Bad Loops
Sometimes loops don’t behave. Even if you have the syntax correct, your loop still may not do what you want. The following sections describe two loop errors: Loops that never happen and loops that never quit.
Managing the reluctant loop
You may write some code and find that the loop never seems to run, as in the following