Advanced Control Structures

This article will cover advanced control structures and techniques, including multi-conditional if statements and an introduction to the for statement.

The 'for' loop

In our previous articles, we discussed using a while loop as a means to repeat a block of code until the condition it provided is determined to be false. Although useful, while loops are used primarily when you donít initially know how many times you want to execute the code block. The for statement is another repetition statement designed to be used when executing a code block a specific number of times. The syntax for a for loop is:

For(initialization; condition; increment) {
// Code to loop
}

Unlike the while loop, a for loop contains three parts separated by semicolons. When a for statement executes, the initialization statement is automatically executed. Then, once the initialization statement has finished, the condition provided is evaluated. If this condition is true, the code block enclosed within the for statement is then executed. After the code within the loop has been executed, the increment statement is evaluated and the condition is then re-evaluated. Note that in a for loop, the initialization statement is only evaluated once -- before any code within the loop is evaluated -- but the increment portion is evaluated after each iteration of the loop. This looping process (evaluate condition, evaluate block, evaluate increment statement) is continued until the conditions provided are false and the loop ends (See Figure 1).

Figure 1. Processing of a "for" loop.

Now that we have an idea of how a for statement works, let's see it in action by looking at the earlier example we used to demonstrate the workings of a while loop where we wanted to display a count of all the numbers between 1 and 5:

<?php
for($L = 1; $L <= 5; $L++) {
echo $L."<br />";
}
?>

As expected, the output is identical to the while loop and displays the numbers 1 through 5. Again note that the increment statement ($L++) could be any valid math statement. For example, we could count backwards from 5 to 1 by using the following:

<?php
for($L = 5; $L >= 1; $L--) {
echo $L."<br />";
}
?>

Multi-conditional 'if' statements

In programming, it is often necessary to execute a piece of code based on more than one condition. To accomplish this, PHP provides a few ways to evaluate a code block based on multiple conditions. Although the desired result can be achieved by simply embedding if statements within other if statements, this is often an excessive and confusing way to accomplish the desired effect. To help remedy this problem, special conditional operators "and" (&&) and "or" (||) are provided. These operators separate multiple conditions within an if statement and determine the final outcome of an evaluation. For example, consider the following statement:

If Billy has $5, and his mom says itís okay, he can have a candy bar.

We already learned how to code this conditional statement by embedding two if statements as shown below:

As you can see, we have used the "and" operator to combine the original two separate conditional statements into a single conditional statement.

Note: The use of parentheses in the above example is to ensure that each separate conditional statement is evaluated properly before the results of those conditions are evaluated through the "and" operator.

If all conditions that are combined using the "and" operator must evaluate to "true" for the statement to be true, then when using the "or" operator only a single condition of the combined conditions must evaluate to "true" in order for the statement to be true. For example, if we modified our example to read:

In this example, the complexity and inefficiency of our code increases because we have to duplicate code twice. To make this code more efficient, we can combine these two conditional statements using our new conditional operators.

The same statement using these operators would look something like this: