Compound Logical Operators

There are many occasions when we need to extend the conditions that are to be tested. Often there are conditions to be linked.

In everyday language we say things like If I had the time and the money I would go on holiday. The and means that both conditions must be true before we take an action. We might also say I am happy to go to the theatre or the cinema. The logical link this time is or . Conditions in if statements are linked in the same way. Conditions linked with and only result in an action when all conditions are true. For example, if a >b and a > c then display “a is the largest”. Conditions linked with an or lead to an action when either or both are true.

Example 1: The program is to input a examination mark and test it for the award of a grade.The mark is a whole number between 1 and 100.

Grades are awarded according to the following criteria:

>= 80 Distinction
>= 60 Merit
>= 40 Pass
< 40 fail

The pseudo-code is

Use variables: mark of type integer
If mark >= 80 display “distinction”
If mark >= 60 and mark < 80 display “merit”
If mark >= 40 and mark < 60 display “pass”
If mark < 40 display “fail”

An if statement on its own is often not the best way of solving problems. A more elegant set of conditions can be created by adding an else statement to the if statement. The else statement is used to deal with situations as shown in the following examples.

Example 7: A person is paid at top for category 1 work otherwise pay is at normal rate.

If the work is category 1
pay-rate is top
pay-rate is normal

The else statement provides a neat way of dealing with alternative condition. In pseudocode we write,

If work = cat1 then p-rate: = top
Else p-rate = normal


If work = cat1 then
p-rate: = top
p-rate = normal

The following example illustrate the use of if … else statements in implementing double alternative conditions. 

If salary < 50000 then
Tax = 0
If salary > 50000 AND salary < 100000 then
Tax = 50000 * 0.05
Tax = 100000 * 0.30

The case statement

Repeating the if … else statements a number of times can be somewhat confusing. An alternative method provided in a number of languages is to use a selector determined by the alternative conditions that are needed. In our pseudo-code, this will called a case statement.

Example 8: The following program segment outputs a message to the monitor screen describing the insurance available according to a category input by the user.

Use variables: category of type character
Display “input category”
Accept category
If category = U
Display “insurance is not available”
If category = A then
Display “insurance is double”
If category = B then
Display “insurance is normal”
If category = M then
Display “insurance is medically dependent”
Display “entry invalid”

This can be expressed in a case statement as follows:

Use variables: category of type character
Display “input category”
Accept category
DO case of category
CASE category = U
Display “insurance not available”
CASE category = A
Display “insurance is double”
CASE category = B
Display “insurance is normal”
CASE category = M
Display “insurance is medically dependent”
Display “entry is invalid”

Instead of using the word otherwise, one can use else.


Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

buttons=(Accept !) days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !