The conditions seen so far are referred to as simple conditions. Multiple simple conditions that are combined are called Compound Conditions. The Boolean operators Not, And and Or are used to form compound conditions.
The equivalent Boolean Operators in pseudo code are NOT, AND, OR. The Boolean Operators in Basic are Not, And, Or. Boolean operators act on conditions, returning either True or False.
Operands are the things operated on by the Operator. For example, the And operator is places between its two operands e.g. operand1 AND operand2. The condition: 10 > 5 AND 5 > 2 has two operands, the first operand is the result of 10 > 5 (which is True), the second operand is the result of 5 > 2 (which is True), so the condition becomes True AND True which results in True.
| Operator | Description | Precedence | Example |
| Not | Not reverses the condition | 1 | Not 10 > 5 = Not True = False Not 5 > 10 = Not False = True |
| And | And results in True if both operands are true, otherwise it is False | 2 | 10 > 5 And 5 > 2 = True And True = True 10 > 5 And 5 > 7 = True And False = False 10 > 12 And 5 > 2 = False And True = False 10 > 12 And 5 > 7 = False And False = False |
| Or | Or results in True if either or both operands are true, otherwise it is False | 3 | 10 > 5 Or 5 > 2 = True Or True = True 10 > 5 Or 5 > 7 = True Or False = True 10 > 12 Or 5 > 2 = False Or True = True 10 > 12 Or 5 > 7 = False Or False = False |
Assume i = 3 and j = 5 in the following:
IF i > =3 And j = 2 THEN
i >= 3 And j = 2
IF i > =3 Or j = 2 THEN
i >= 3 Or j = 2
Like arithmetic operators, boolean operators have an evaluation order (order of precedence). Firstly working form left to right, NOT is evaluated, then AND then OR.
Operator precedence refers to the order that operators are evaluated in an expression. Operators of equal precedence are evaluated from left to right. Brackets (Parenthesis) can be used to change the order of precedence of expressions.
| Precedence | Operators |
| 1 (Highest) | () |
| 2 | - (minus sign) |
| 3 | *, / |
| 4 | +, - |
| 5 | & (string concatenation) |
| 6 | =, <>, < <=, >, >= |
| 7 | Not |
| 8 | And |
| 9 (Lowest) | Or |
Assume i = 3 and j = 5 in the following:
IF i > j And i = 4 Or Not i <= 10 + 4 * j THEN
The order of evaluation is: *, +, >, =, <=, Not, And, Or
j > i And i = 4 Or Not i <= 10 + 4 * j
Problem 1: Display whether a month number is valid (between 1 and 12 inclusive) or not.
Pseudo Code
checkMonthNumber()
Input monthNumber
IF monthNumber >= 1 AND monthNumber <= 12 THEN
Display "Valid month number"
ELSE
Display "Invalid month number"
ENDIF
STOP
Problem 2: Display if it is a weekend or not
Pseudo Code
displayDay()
Input day
IF day = "Saturday" OR day = "Sunday" THEN
Display "Weekend"
ELSE
Display "Weekday"
ENDIF
STOP
The pseudo code for the tax problem from the last lecture could be written as:
Pseudo Code
calcTax()
Input income
IF income <= 6000 THEN
tax = 0
ENDIF
IF income > 6000 AND income <= 20000 THEN
tax = (income - 6000) * 0.17
ENDIF
IF income > 20000 AND income <= 50000 THEN
tax = 2380 + (income - 20000) * 0.30
ENDIF
IF income > 50000 AND income <= 60000 THEN
tax = 11380 + (income - 50000) * 0.42
ENDIF
IF income > 60000 THEN
tax = 15580 + (income - 60000) * 0.47
ENDIF
Display tax
STOP
Why is this a less efficient solution?
Why is there greater potential for logic errors?
The Pseudo Code for the tax problem could also be written as:
calcTax()
Input income
IF income <= 6000 THEN
tax = 0
ELSE
IF income > 6000 AND income <= 20000 THEN
tax = (income - 6000) * 0.17
ELSE
IF income > 20000 AND income <= 50000 THEN
tax = 2380 + (income - 20000) * 0.30
ELSE
IF income > 50000 AND income <= 60000 THEN
tax = 11380 + (income - 50000) * 0.42
ELSE
IF income > 60000
tax = 15580 + (income - 60000) * 0.47
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
Display tax
STOP
What parts of the above conditions are redundant? Why?
The cost at certain times of the day are shown below:
| Day | Time (24 hour) | Cost per Minute ($) |
| Weekday | 7:00 to 19:00 | 0.30 |
| Weekday | before 7:00 or after 19:00 | 0.15 |
| Saturday | all day | 0.10 |
| Sunday | all day | 0.10 |
Run Day (full day name) ? Monday Duration (minutes) ? 10 Time (hour) ? 14 Call Cost = $3.00
Run Day (full day name) ? Friday Duration (minutes) ? 10 Time (hour) ? 20 Call Cost = $1.50
Run Day (full day name) ? Saturday Duration (minutes) ? 10 Call Cost = $1.00 Run Day (full day name) ? Sunday Duration (minutes) ? 10 Call Cost = $1.00
Defining Diagram
Outline the Solution
Pseudo Code
calcCallCost() STOP
Data Dictionary
| Name | Data Type | Description |
| callCost | Double | The cost of the call in $. |
| costPerMinute | Double | The cost of a call per minute. |
| day | String | The day the call was made eg "Saturday" |
| duration | Double | The duration of the call in minutes |
| time | Integer | The hour the call was made (in 24 hour time) e.g. 5 is 5am, 19 is 7pm |
Discussion
Unlike the previous nested IF examples, the IF statements use different variables in the conditions. Care needs to be taken that all possible combinations of values are covered e.g. all day and time combinations. This is also an example of using compound conditions.
Why were Saturday and Sunday dealt with before the weekdays?
isItHot()
Input temperature, humidity, windDirection
IF temperature > 40 THEN
Display "Very Hot"
ELSE
IF humidity > 80 AND temperature > 35 THEN
Display "Hot and steamy"
ELSE
IF temperature > 30 AND (windDirection = "N" OR windDirection = "NW") THEN
Display "Hot"
ENDIF
ENDIF
ENDIF
STOP
Discussion
There are gaps in the logic shown for the isItHot problem above. What are they?
Many of the problems above involved a single variable from which the value of another variable could be determined e.g. tax could be determined solely from income. Once a condition was founded to be true, the appropriate statement(s) were executed then the IF statement was exited - there was no need to examine any more conditions.
However in some problems the value of a variable (or variables) must be re-examined in multiple separate IF statements. The IF statements are independent - the result of one condition does not affect the outcome of another condition. More than one of the conditions may be true, logically the conditions overlap.
A holiday resort has rooms for $200 per night, or if customers stay for 1 or more weeks they are charged $1200 per week (7 days for the price of 6). In addition if the customer is a member of an automobile club, they receive an additional 5% off regardless of the duration of the stay. Assume that customers stay 1 to 6 days or 1 week, 2 weeks, ...
Run Number of Nights (1 to 6, 7, 14, 21, ...)? 5 Member of Auto Club (Y=Yes, N=No) ? N Cost = $1000
Run Number of Nights ? 7 Member of Auto Club (Y=Yes, N=No) ? N Cost = $1200
Run Number of Nights ? 7 Member of Auto Club (Y=Yes, N=No) ? Y Cost = $1140
Defining Diagram
roomCost() STOP
In the following problem, it is possible for a number to be positive, even, and big all at the same time.
Example Run
Run Number ? 12345678 The number 12345678 is: positive or zero. even. big.
Run Number ? -3 The number -3 is: negative or zero. odd.
Pseudo Code
numberInfo()
message = ""
Input number
IF number = 0 THEN
message = message + "zero. "
ENDIF
IF number >= 0 THEN
message = message + "positive or zero. "
ENDIF
IF number <= 0 THEN
message = message + "negative or zero. "
ENDIF
IF number is even THEN
message = message + "even. "
ELSE
message = message + "odd. "
ENDIF
IF number > 1000000 THEN
message = message + "big. "
ENDIF
Display "The number ", number, " is: ", message
STOP
Written by Tim Whitfort.