Using the if Statement to Control Program Flow

Using the if Statement to Control Program Flow

One of the most interesting things computers do is appear to make decisions. The decision-making ability of the computer is really an illusion. The programmer stores very specific instructions inside a computer, and it acts only on those instructions. The simplest form of this behavior is a structure called the if statement.

Introducing the Ace Program

I'll slightly modify the "roll-em" program to illustrate how it can be improved with an if structure. Figure 3.3 shows the program when the program rolls any value except one.

Click To expand
Figure 3.3: When the roll is not a one, nothing interesting happens.

However, this program does something exciting (okay, moderately exciting) when it rolls a one, as you can see from Figure 3.4.

Click To expand
Figure 3.4: When a one appears, the user is treated to a lavish multimedia display.

Creating a Condition

On the surface, the behavior of the Ace program is very straightforward: it does something interesting only if the die roll is one, and it doesn't do that interesting thing in any other case. While it is a simple idea, the implications are profound.

The same simple mechanism used in the Ace program is the foundation of all complicated computer behavior, from flight simulators to heart monitors. Take a look at the code for the Ace program and see if you can spot the new element:

<html>
<head>
<title>Ace!</title>
</head>
<body>
<h1>Ace!</h1>
<h3>Demonstrates if statement</h3>

<?
$roll = rand(1,6);
print "You rolled a $roll";

if ($roll == 1){
  print "<h1>That's an ace!!!!!</h1>";
} // end if

print "<br>";
print "<img src = die$roll.jpg>";
?>
<br>
Refresh this page in the browser to roll another die.

</body>
</html>

The secret to this program is the segment that looks like this:

if ($roll == 1){
  print "<h1>That's an ace!!!!!</h1>";
} // end if

The line that prints "That's an ace!" will not happen every time the program is run. It will only happen if a certain condition is true. The if statement sets up a condition for evaluation. In this case, the condition is read "$roll is equal to one." If that condition is true, all the code between the left brace ({) and the right brace (}) will evaluate. If the condition is not true, the code between the braces will be skipped altogether.

A condition can be thought of as an expression that can be evaluated as true or false. Any expression that can return a true or false value can be used as a condition. Most conditions look much like the one in the Ace program. This condition checks the variable $roll to see if it is equal to the value 1.

Note that equality is indicated by two equals signs (==).

This is important, because computer programs are not nearly as flexible as humans. We humans often use the same symbol for different purposes. While computer languages can do this, it often leads to problems. The single equals sign is reserved for assignment. You should read this line

$x = 5;

as x gets five, indicating that the value five is being assigned to the variable $x. The code fragment

$x == 5;

should be read as x is equal to five, as it is testing equality. It is essentially asking whether x is equal to five. A condition such as $x == 5 does not stand on its own. Instead, it is used inside some sort of other structure, such as an if statement.

Exploring Comparison Operators

Equality (==) is not the only type of comparison PHP allows. There are several other ways to compare a variable and a value or two variables. These comparison operators are described in table 3.1

Table 3.1: COMPARISON OPERATORS

Operator

Description

==

equal to

<

less than

>

greater than

<=

less than or equal to

>=

greater than or equal to

!=

not equal to

These comparison operators work on any type of data, although the results might be a little strange. For example, if you have a condition like

"a" < "b"

you would get the result true because alphabetically, the letter "a" is earlier than "b," so it has a "smaller" value.

Creating an if Statement

An if statement begins with the keyword if followed by a condition inside parentheses. After the parentheses is a left brace ({). You can put as many lines of code between the left brace and the right brace(}) as you wish. Any code between the braces will only be executed if the condition is true. If the condition is false, program control flows to the next line after the right brace. It is not necessary to put a semicolon on a line ending with a brace. It is customary to indent all the code between the left brace and the right brace.

TRAP?

Do not put a semicolon at the end of the if line. The following code

if ("day" == "night") ; {
  print "we must be near a black hole";
} // end if

will print "we must be near a black hole." When the processor sees the semicolon following ("day" == "night"), it thinks there is no code at all to evaluate if the condition is true, so the condition is effectively ignored. Essentially, the braces are used to indicate that an entire group of lines are to be treated as one structure, and that structure is part of the current logical line.