Control Statements

Page 9
By Aaron Weiss (Send Email)
Posted Jul 7, 2000


the "if ... else" statement

A programming classic -- the if ... else statement is u sed to execute a block of code when a given condition is true. Optionally, you can execute an alternative block of code if the condi tion is false, or test additional conditions.

if (conditional expression) { ...code to execute if true...
}
el se {
...code to execute if false...
}

For instance, you might print out a special welcome only if the user su bmitted the name "Harry":

if ($name == "Harry) {
print "Hello Harry, my name is also Harry!";< br>}

Perhaps you set a shipping rate of $5 for total orders below or equal to $25, and $10 for total orders above $25:

if ($total <= 25) {
$total += 5;
}
else {
$total += 10;
}

An optional clause, elseif, lets you test alternative conditions before dropping out to the else clause. For example, suppose the shipping rate is $5 for orde rs of $25 and below, $10 for orders above $25 up to $50, and $15 for orders above $50.

if ($total <= 25) {
$total += 5;
}
elseif ($total <= 50) {
$total += 10;
}
else {
$total += 15;
}

Notice that the elseif clause will only be evaluated if the first condition ($total <= 25) should fail, so we know that $total is at least 26 b y the time we evaluate whether it is less than or equal to 50.

the "while" statement

Another classic programming technique is the loop, where one or more statements are executed repeatedly either while or un til a certain condition is met. A while loop is probably the simplest loop statement, in PHP or any other language for that m atter. Let''s count to 100:

$counter = 0;
while ($counter <= 100) {
print "$counter<BR>";
$co unter++;
}

On each pass, or iteration as they say, through the while loop the condition is tested. As long as the condition remains true, the statements inside the while loop (inside the curly braces which define the "statement block ") are executed. Notice that this script will output the numbers 0 to 100, because $counter is not incremented to 1 unti l the end of the first pass. Also note that if the condition were false initially, the statements inside the while loop would never be executed -- which may be fine, depending on the scenario. The important thing about a loop such as while is that th e condition must eventually prove false, otherwise the loop will never end and very bad things may happen -- the computer cou ld lock up, become unresponsive, or the programming language may produce an error. Because we increment $counter on each pass , it will eventually surpass 100, causing the condition to become false.

 

the "do...while" stat ement

Very similar to while, this variation simply moves the conditional check to the end of the statement blo ck:

 $counter = 0;
do {
print "$counter<BR>";
$counter++;
}
while ($counter <= 100);

In this case the output would be exactly the same as the output from our while example -- the numbers 0 to 100. The di fference is that if the condition proved false on the first pass, the statement block would have been executed at least once, as opp osed to never in the case of the while statement.

 

the "for" statement

In many respects the for loop is similar to while, except that all parameters are set forth at the outset. These for statements are often used to repeat a sequence a given number of times. Repeating our example, suppose we want to output a count from 0 to 100:

for ($counter = 0; $counter <= 100; $counter++) {
print "$counter<BR>";
}

One typically sets up three parameters in the for statement, as seen above: The first sets an initial value for the conditio nal variable; the second specifies the condition to test; the third modifies the conditional variable on each iteration, or pass thr ough the loop.

gate hopping with "break" and "continue"

You can further modify prog ram flow within a conditional loop using PHP''s break and continue statements. The break statement will immedia tely exit the loop, regardless of conditions, while continue will skip ahead to the top of the loop and the condition test, i gnoring any remaining statements in the statement block for the given pass. Let''s return to our for statement example, but s uppose we want to output only those numbers divisible by two.

for ($counter = 0; ; $counter++) { if (($counter % 2) != 0) {

continue;
}
print "$counter<BR>";
if ($counter >= 100) {
break;
} }

Several poi nts are illustrated above. First, we can eliminate the condition from the for statement. This will cause the loop to repeat i ndefinitely unless there is a break statement somewhere to quit the loop, which is exactly what we''ve done. We''ve tested th e condition within the statement block, and if the counter surpasses our limit, the break statement is executed. Also notice the first if statement in the statement block: the conditional test compares the value of $counter modulus 2 (the rema inder of this division) to zero. If the modulus is not zero, then we know that the counter value was not divisible by two and we iss ue the continue statement. This will skip the rest of the statements and return to the for, resulting in no output for that pass.

 


NEXT ->



Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.