Decision Structure

A decision structure (or selection structure) allows a program to perform actions only under certain conditions.

A control structure is a logical design that controls the order in which a set of statements executes. Up until this point, we've only used the simplest type of control structure: the sequence structure - set of statements that execute in the order they appear. Some problems simply cannot be solved by performing a set of ordered steps, one after the other.

For example, a pay calculating program that determines whether an employee worked overtime would need to calculate pay at a higher rate for overtime hours, but calculate pay at the regular rate for regular hours. These types of programs require a different type of control structure: one that can execute a set of statements only under certain circumstances. This can be accomplished with a decision structure.

The simplest form of a decision structure would have a specific action performed ONLY if a certain condition exists. If the condition does not exist, the action is not performed. For example, if it is cold outside, we would wear a coat, but if not, we would not wear the coat. This is known as a single alternative decision structure because it provides only one alternative path of execution.

Using a flowchart, we use a diamond symbol to represent a true/false condition.

Flowchart

If the condition in the diamond symbol is true, the program takes the alternative path. Otherwise, it exits the structure. The diamond symbol indicates that some condition must be tested in a flowchart.

Combining Structures

We can not use a decision structure alone to create a complete program. We use the decision structure to test a condition and conditionally execute based on the outcome of the decision. For other parts of the program, we use other structures. Often, structures are nested inside of other structures. For example, the flowchart below nests a sequence structure (wear coat and wear gloves) inside the decision structure (Cold Outside?).

Flowchart

Writing Decision Structures in Pseudocode

We begin a decision structure with the If clause and end it with an End If clause. In between, we list the statements that will be executed if the condition is true. These statements are called the conditional statements. For example

If condition Then
    statement
    statement
    etc.
End If

Boolean Expressions and Relational Operators

A Boolean expression is an expression that can be evaluated as either true or false. Boolean expressions are named after the mathematician George Boole who, in the 1800s, invented a system of mathematics in which the abstract concepts of true and false can be used in computations. Hence, the condition that is tested by an If - Then statement must be a Boolean expression.

A Boolean expression that is tested by an If - Then statement is formed with a relational operator. A relational operator determines whether a specific relationship exists between two values. For example, the greater than operator (>) determines whether one value is greater than another. The equal to operator (==) determines where two values are equal. The table below lists the relational operators commonly available in most programming languages.

Relational Operators
OperatorMeaning
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
==
Equal to
!=
Not equal to

The following expression checks to see if hoursWorked is greater than 40

hoursWorked > 40
This expression will be either true or false depending upon if hoursWorked is greater than 40. Because it can only be true or false, it is a Boolean expression. Let's look at some examples of Boolean expressions.

Boolean Expressions Using Relational Operators
ExpressionMeaning
 x > y  is x greater than y?
 x < y  is x less than y?
 x >= y  is x greater than or equal to y?
 x <= y  is x less than or equal to y?
 x == y  is x equal to y?
 x != y  is x not equal to y?

Notice that the expressions test whether the operand on the left is (>, <, >=, <=, ==, or !=) the operand on the right. We use two = characters as the equal to operator to avoid confusion with the assignment operator, which is one = character. Java, Python, C, and C++ also make this distinction. Some languages do not. The not equal to operator != is used by Java, C, and C++. However, Visual Basic uses <> instead.

Indentation

In order to make things easy to read, make sure the If clause and the End If clause are aligned vertically - indented the same amount. Also, be sure to indent the conditionally executed statements as follows

If hoursWorked > 40 then
    Display "You get overtime pay!"
    Display "Yippie!"
End If

STOP! Take a close look at the "In the Spotlight" on pages 130-132.

Dual Alternative Decision Structures

A dual alternative decision structure will execute one group of statements if its Boolean expression is TRUE, or another group if it is FALSE. We simply add an Else clause to the IF statement. Align the Else vertically with the If clause and the End If clause as follows

If hoursWorked > 40 then
    Display "Yippie! I get overtime pay!"
Else
    Display "No overtime for me :-("
End If

STOP! Take a close look at the "In the Spotlight" on pages 134-138.