Search This Blog

Decision Structures and Boolean Logic


Introduction to Decision Structures



CONCEPT: A decision structure (which is also known as a 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.



Although the sequence structure is heavily used in programming, it cannot handle every type of task. Some problems simply cannot be solved by performing a set of ordered steps, one after the other. For example, consider a pay calculating program that determines whether an employee has worked overtime. If the employee has worked more than 40 hours, he or she gets paid extra for all the hours over 40. Otherwise, the overtime calculation should be skipped. Programs like this 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. (Decision structures are also known as selection structures.)



Writing a Decision Structure in Pseudocode


In pseudocode we use the If-Then statement to write a single alternative decision structure. Here is the general format of the If-Then statement:
An example of a if then psuedo code.





We will refer to the line that begins with the word If as the If clause, and we will refer to the line that reads End If as the End If clause. In the general format, the condition is any expression that can be evaluated as either true or false. When the If-Then statement executes, the condition is tested. If it is true, the statements that appear between the If clause and the End If clause are executed. The End If clause marks the end of the If-Then statement.

Boolean Expressions and Relational Operators

All programming languages allow you to create expressions that can be evaluated as either true or false. These are called Boolean expressions, named in honor of the English mathematician George Boole. In the 1800s Boole invented a system of mathematics in which the abstract concepts of true and false can be used in computations. The condition that is tested by an If-Then statement must be a Boolean expression.


Typically, the 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 whether two values are equal. Table 4.1 lists the relational operators that are commonly available in most programming languages.












The following is an example of an expression that uses the greater than (>) operator to compare two variables, length and width:

length > width

This expression determines whether the value of length is greater than the value of width. If length is greater than width, the value of the expression is true. Otherwise, the value of the expression is false. Because the expression can be only true or false, it is a Boolean expression. The following expression uses the less than operator to determine whether length is less than width:

length < width


The >= and <= Operators


Two of the operators, >= and <=, test for more than one relationship. The >= operator determines whether the operand on its left is greater than or equal to the operand on its right. For example, assuming that a is 4, b is 6, and c is 4, both of the expressions b >= a and a >= c are true, and the expression a >= 5 is false.

The <= operator determines whether the operand on its left is less than or equal to the operand on its right. Once again, assuming that a is 4, b is 6, and c is 4, both a <= c and b <= 10 are true, but b <= a is false.
The == Operator

The == operator determines whether the operand on its left is equal to the operand on its right. If both operands have the same value, the expression is true. Assuming that a is 4, the expression a == 4 is true and the expression a == 2 is false.

The != Operator


The != operator is the not equal to operator. It determines whether the operand on its left is not equal to the operand on its right, which is the opposite of the == operator. As before, assuming a is 4, b is 6, and c is 4, both a != b and b != c are true because a is not equal to b and b is not equal to c. However, a != c is false because a is equal to c.

Note that != is the same character sequence used by several languages for the not equal to operator, including Java, C, and C++. Some languages, such as Visual Basic, use <> as the not equal to operator.
Putting It All Together

Let’s look at the following example of the If-Then statement in pseudocode:

If sales > 50000 Then
   Set bonus = 500.0
End If

This statement uses the > operator to determine whether sales is greater than 50,000. If the expression sales > 50000 is true, the variable bonus is assigned 500.0. If the expression is false, however, the assignment statement is skipped.


 If sales > 50000 Then
   Set bonus = 500.0
   Set commissionRate = 0.12
   Display "You've met your sales quota!"
End If


The following pseudocode uses the == operator to determine whether two values are equal. The expression balance == 0 will be true if the balance variable is set to 0. Otherwise the expression will be false.

If balance == 0 Then
   // Statements appearing here will
   // be executed only if balance is
   // equal to 0.
End If


The following pseudocode uses the != operator to determine whether two values are not equal. The expression choice != 5 will be true if the choice variable is not set to 5. Otherwise the expression will be false.

If choice != 5 Then
   // Statements appearing here will
   // be executed only if choice is
   // not equal to 5.
End If

 

In the Spotlight:


Using the If-Then Statement


Kathryn teaches a science class and her students are required to take three tests. She wants to write a program that her students can use to calculate their average test score. She also wants the program to congratulate the student enthusiastically if the average is greater than 95. Here is the algorithm:

    1. Get the first test score.

    2. Get the second test score.

    3. Get the third test score.

    4. Calculate the average.

    5. Display the average.

    6. If the average is greater than 95, congratulate the user.



Program 4-1

 1  // Declare variables
2  Declare Real test1, test2, test3, average
3
 4  // Get test 1
5  Display "Enter the score for test #1."
6  Input test1
7
 8  // Get test 2
9  Display "Enter the score for test #2."
10  Input test2
11
12  // Get test 3
13  Display "Enter the score for test #3."
14  Input test3
15
16  // Calculate the average score.
17  Set average = (test1 + test2 + test3) / 3
18
19  // Display the average.
20  Display "The average is ", average
21
22  // If the average is greater than 95
23  // congratulate the user.
24  If average > 95 Then
25     Display "Congratulations! Great average!"
26  End If

Program Output (with Input Shown in Bold)


Enter the score for test #1.
82 [Enter]
Enter the score for test #2.
76 [Enter]
Enter the score for test #3.
91 [Enter]
The average is 83

Program Output (with Input Shown in Bold)


Enter the score for test #1.
93 [Enter]
Enter the score for test #2.
99 [Enter]
Enter the score for test #3.
96 [Enter]
The average is 96
Congratulations! Great average!



Click Dual Alternative Decision Structures INFO Next Page ----------------------->

Categories:

Leave a Reply