If Statements

November 19th, 2009

A low-level way of steering program flow is using an if-statement, denoted by the keywords if, else if and else. If-statements test a series of conditions until one of the conditions is satisfied, or evaluates to true. When a condition is true, a statement or code block associated with that condition is executed.

Informally written in pseudo code (an imaginary language), an if statement looks like:

if (condition1) then
    // If condition1 is true then statement1 is run
    statement1;
else if (condition2) then
    // if condition2 is true then statement2 is run
    statement2;
else if (condition3) then
    // if condition3 is true then statement3 is run
    statement3;
else
    // if none of the above conditions are true then statement4 is run
    statement4;
end if

Below is an if statement written in PHP that tests a single condition.

If Example (Condition True)

$desiredTemperature = 80;
$roomTemperature = 90;

if ($roomTemperature > $desiredTemperature) {
        echo("It's hot in here.  Turn on the A/C.\n");
}

echo("Done.\n");

The condition tested is ($roomTemperature > $desiredTemperature). Substituting the values of the variables we have (90 > 80). 90 is greater than 80, and it said that this condition is true. When a condition is true, the code block enclosed in { } symbols is executed.

The above program outputs:

It's hot in here. Turn on the A/C.
Done.

If Example (Condition False)

When I set $roomTemperature to 70 in the example below, the condition tested is (70 > 80). 70 is not greater than 80. The condition tested is false and code block immediately following the condition is not executed.

$desiredTemperature = 80;
$roomTemperature = 70;

if ($roomTemperature > $desiredTemperature) {
        echo("It's hot in here.  Turn on the A/C.\n");
}

echo("Done.\n");

The program output is:

Done.

Flow Diagrams

For those visual individuals out there, flow diagrams graphically illustrate high-level program execution. Diamond symbols indicate that a decision must be made. In the low-level example below, I place the concept tested in a diamond.

if-flow-diagram-1

The condition in brackets [ ] indicates that the program flow should be directed to “Too Hot” if the condition is true.

If, Else If Example

When the first condition tested evaluates to false, the computer will attempt to evaluate the next condition indicated by the keyword else if.

$desiredTemperature = 80;
$roomTemperature = 50;

if ($roomTemperature > $desiredTemperature) {
        echo("It's hot in here.  Turn on the A/C.\n");
}
else if ($roomTemperature < $desiredTemperature - 10) {
        echo("It's freezing in here.  Turn on the heater.\n");
}

echo("Done.\n");

The room temperature is changed to 50. In first condition (50 > 80) is not true, and the computer sees that there is a second condition which tests if the room temperature is ten degrees cooler than the desired temperature. (50 < 80 - 10) is (50 < 70) which is true, and the code block containing the echo statement "It's freezing in here..." is run.

The program output is:

It’s freezing in here. Turn on the heater.
Done.

The corresponding flow diagram for the above if-else if statement becomes.

if-flow-diagram-2

Flow diagrams illustrate all paths that a program can take.

If, Else If, Else If Example

The example below introduces a third condition, testing if the mercury level meets the desired comfort level of this picky person.

$desiredTemperature = 75;
$roomTemperature = 75;

if ($roomTemperature > $desiredTemperature) {
        echo("It's hot in here.  Turn on the A/C.\n");
}
else if ($roomTemperature < $desiredTemperature - 10) {
        echo("It's freezing in here.  Turn on the heater.\n");
}
else if ($roomTemperature == $desiredTemperature) {
        echo("The temperature is just fine.  Thanks!\n");
}

echo("Done.\n");

What happens?

  1. The first condition (75 > 75) is false. The computer moves forward to the next else if.
  2. The second condition (75 < 65) is false. The computer moves forward to the next else if.
  3. The third condition (75 == 75) is true. The computer executes the echo statement with "The temperature is just fine..."

Program output:

The temperature is just fine. Thanks!
Done.

The Equality Operator ==

You asking yourself, "Why don't you use the equals sign = for equality?"

The equals sign is reversed for variable assignment, i.e.

$desiredTemperature = 75;

Attempting to do else if ($roomTemperature = $desiredTemperature) sets to $roomTemperature to the value of $desiredTemperature, 75. The else if condition evaluates to (75). This is shorthand for, "is 75 a non-zero number?" This condition always evaluates to true unless $desiredTemperature is zero.

The equality operator == is a binary operator that tests if the values on both sides of the operator are equal.

The corresponding flow diagram for the above conditional is as follows.

if-flow-diagram-3

Since I am limited to three output paths per diamond, I use a separate diamond for each condition.

If, Else If, Else Example

Should none of the conditions in if statements and else if statements not evaluate to true, the computer unconditionally runs the statement or code block following the else keyword. An if-else combination is also valid.

The else keyword must be at the end of an if, else-if chain.

$desiredTemperature = 75;
$roomTemperature = 73;

if ($roomTemperature > $desiredTemperature) {
        echo("It's hot in here.  Turn on the A/C.\n");
}
else if ($roomTemperature < $desiredTemperature - 10) {
        echo("It's freezing in here.  Turn on the heater.\n");
}
else if ($roomTemperature == $desiredTemperature) {
        echo("The temperature is just fine.  Thanks!\n");
}
else {
        echo("I don't know.\n");
}

echo("Done.\n");

What happens?

  1. The first condition (73 > 75) is false. The computer moves forward to the next else if.
  2. The second condition (73 < 65) is false. The computer moves forward to the next else if.
  3. The third condition (73 == 75) is false. The computer moves forward to the next else if.
  4. There are no more else-if conditions. The code block in the else condition is run.

The program output is:

I don't know.
Done.

And the corresponding flow diagram.

if-flow-diagram-4

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>