HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [145]
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.
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.
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:
for loops are based on an integer, which is sometimes called a sentry variable. In this example, lap serves as the sentry variable. You typically use the sentry variable to count the number of repetitions through a loop.
The for statement has three distinct parts:
♦ Initialization: This segment (lap = 1) sets up the initial value of the sentry.
♦ Condition: The condition (lap <= 10) is an ordinary condition (although it doesn’t require parentheses in this context). As long as the condition is evaluated as true, the loop will repeat.
♦ Modification: The last part of the for structure (lap++) indicates how the sentry will be modified throughout the loop. In this case, I add 1 to the lap variable each time through the loop.
The for structure has a pair of braces containing the code that will be repeated. As usual, all code inside this structure is indented. You can have as much code inside a loop as you want.
The lap++ operator is a special shortcut. Adding 1 to a variable is common, so the lap++ operation means “add 1 to lap.” You can also write lap = lap + 1, but lap++ sounds so much cooler.
When programmers decided to improve on the C language, they called the new language C++. Get it? It’s one better than C! Those computer scientists are such a wacky bunch!
When you know how many times something should happen, for loops are pretty useful.
Counting backward
You can modify the basic for loop so that it counts backward. Figure 3-2 shows an example of this behavior.
Figure 3-2: This program counts backward.
The backward version of the for loop uses the same general structure as the forward version (shown in the preceding section), but with slightly different parameters:
If you want to count backward, just modify the three parts of the for statement:
♦ Initialize the sentry to a large number. If you’re counting down, you need to start with a number larger than 0 or 1.
♦ Keep going as long as the sentry is larger than some value. The code inside the loop will execute as long as the condition is true. The number will be getting smaller, so make sure that you’re doing a greater than (>) or greater than or equal to (>=) comparison.
♦ Decrement the sentry. If you want the number to get smaller, you need to subtract something from it. The -- operator is a quick way to do so. It subtracts 1 from the variable.
Counting by 5
You can use the for loop to make other kinds of counting loops. If you want to count by five, for example, you can use the following variation:
This code starts i as five, repeats as long as i is less than or equal to 25, and adds 5 to i on each pass through the loop. Figure 3-3 illustrates this code in action.
Figure 3-3:
for loops can also skip values.
If you want a for