HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [214]
2. Add an initializer.
for loops usually center around a specific integer variable, sometimes called the sentry variable. The first part of the for loop sets up the initial value of that variable. Often, the variable is initialized to 0 or 1.
for ($i = 0; $i < 100; $i++){
3. Add a condition.
The loop will continue as long as the condition is true and will exit as soon as the condition is evaluated as false. Normally, the condition will check whether if the variable is larger than some value.
for ($i = 0; $i < 100; $i++){
4. Add a modifier.
Every time through the loop, you need to do something to change the value of the sentry. Normally, you add 1 to the sentry variable.
for ($i = 0; $i < 100; $i++){
5. Encase the body of the loop in braces.
The code that will be repeated is placed inside braces({}). As usual, indent all code inside braces so you understand that you’re inside a structure.
for loops are first described in Book IV, Chapter 3. Please look to that chapter for more details on for loops, including how to build a loop that counts backward and counts by fives. I don’t repeat that material here because for loops work exactly the same in PHP and JavaScript.
This particular program has a few other features that make it suitable for printing out 100 dice.
♦ It uses $i as a counting variable. When the sentry variable’s name isn’t important, $i is often used. $i will vary from 0 to 99, giving 100 iterations of the loop.
♦ Each time through the loop, roll a die. The familiar rand() function is used to roll a random die value between 1 and 6. Because this code is inside the loop, it is repeated.
$userNumber = rand(1,6);
♦ Print out an image related to the die roll. I use interpolation to determine which image to display. Note that I used CSS to resize my image files to a smaller size.
print <<< HERE

alt = “$userNumber” />
HERE;
♦ Check whether you rolled a 6. For some strange reason, my obsession with sixes continues. If the roll is a 6, add 1 to the $sixCount variable. By the end of the loop, this will contain the total number of sixes rolled.
if($userNumber == 6){
$sixCount++;
} // end if
♦ Print the value of $sixCount. After the loop is completed, report how many sixes were rolled.
print “
You rolled $sixCount six(es)!
”;Looping with while
The while loop is the other primary way of repeating code. Figure 3-9 shows a variation of the die rolling game.
Figure 3-9: This time, the program continues until it gets a 6.
while loops are much like for loops. They require the same thought:
♦ A sentry variable: This special variable controls access to the loop. Unlike the int usually used in for loops, the sentry of a while loop can be any type.
♦ Initialization: Set the initial value of the sentry variable before the loop begins. Do not rely on default settings (because you don’t know what they will be) but set this value yourself.
♦ A condition: The while statement requires a condition. This condition controls access to the loop. As long as the condition is true, the loop continues. As soon as the condition is evaluated as false, the loop will exit.
♦ A modifier: You must somehow modify the value of the sentry variable. It’s important that the modification statement happen somewhere inside the loop. In a for loop, you almost always add or subtract to modify a variable. In a while loop, any kind of assignment statement can be used to modify the variable.
for loops are a little safer than while loops because the structure of the for loop requires you to think about initialization, condition, and modification. All three features are built into the for statement. The while statement requires only the condition. This might make you think that you don’t need the other parts, but that would be dangerous. In any kind of loop, you need to initialize the sentry variable and modify its value. With the while loop, you’re responsible for adding these features