There are a few other types of loops in addition to while loops that we learned about in Part 4.

Do-While Loop

A do-while loop is very similar to a while loop. The only difference is that the condition is evaluated after each loop iteration, rather than before. The syntax is like this:

do {
  // some code here
} while (condition)

Since the condition isn't evaluated until after the loop body, the loop body is always executed at least once, whether or not the condition is true. Once it does get to the condition, if it is true, it goes back up to the top of the loop body. If it is false, the loop terminates.

$i = 11;
do {
   print "The number is $i<br />";
   ++$i;
} while ($i <= 10)

The above code prints:

The number is 11&lt;br /&gt;

even though the condition is not true. The loop body always executes once.

For Loop

The for loop is a bit more complicated. The syntax looks like this:

for (initial-expression; condition; loop-end-expression) {
  //some code goes here
}

The initial-expression is evaluated only once, at the very beginning of the loop. The condition is then evaluated. If the condition is true, the body of the loop executes. Then the loop-end-expression is run. The process then starts over again with evaluating the condition. A for loop is generally used to do simple bounding loops, like this:

for ($i = 1; $i <= 10; ++$i) {
   print "The number is $i<br />"
}

As you'll notice, the same exact thing can be done with a while loop, but many prefer the for loop as it is more compact:

$i = 1;
while ($i <= 10) {
   print "The number is $i<br />";
   ++$i;
}

More Looping Examples

Let's try using a for loops to create a multiplication table.

$start = 1;
$end = 10;

print '<table border="1" cellspacing="0" cellpadding="5">';
for ($y = $start - 1; $y <= $end; ++$y) {
	print "<tr>";
	for ($x = $start - 1; $x <= $end; ++$x) {
		if ($y == $start - 1) {
			print "<td><b>$x</b></td>";
			
		} else if ($x == $start - 1) {
			print "<td><b>$y</b></td>";
		} else {
			print "<td>"
            print $x*$y;
            print "</td>";
		}
	}
	print "</tr>";
}
print "</table>";

Notice that there is a for loop inside of another for loop. These are called nested for loops. The outer loop loops through each row, and while the inner loop loops through each column. This means on every iteration of the outer loop, the entire inner loop runs. Trace the above code carefully to see how it works. This code will produce the following table:

0 1 2 3 4 5 6 7 8 9 10
1

1 2 3 4 5 6 7 8 9 10
2

2 4 6 8 10 12 14 16 18 20
3

3 6 9 12 15 18 21 24 27 30
4

4 8 12 16 20 24 28 32 36 40
5

5 10 15 20 25 30 35 40 45 50
6

6 12 18 24 30 36 42 48 54 60
7

7 14 21 28 35 42 49 56 63 70
8

8 16 24 32 40 48 56 64 72 80
9

9 18 27 36 45 54 63 72 81 90
10

10 20 30 40 50 60 70 80 90 100

Test for Understanding

  1. In the code above how many times does line 6 run?
  2. In the code above how many times does line 8 run?

Answers:

  1. Line 6 runs while $y <= 10. $y starts at 0 ($start - 1) and goes up by 1 each time. So it runs while $start = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. That's 11 times!
  2. Line 8 runs on every iteration of the inner loop. The inner loop runs while $x <= 10. $x starts at 0 and goes up by 1 each time. So the inner loop runs 11 iterations. But the inner loop happens again on every iteration of the outer loop, which also iterates 11 times. 11 x 11 = 121 times.
Be Sociable, Share!