Logical Operators - AND and OR statements
Back to Main Page
Until now, we have used If...Then statements to make decisions but they only allow ONE condition to be tested, for example

If Age > 17 Then.....do something

What if we want
2 or more conditions to be met? If a student has to pass an exam AND a written assignment to pass a unit on their course, a simple If...Then doesn't allow for this.

We need to use
logical operators - the most common ones are AND and OR statements


AND Conditions

For an AND statement to be 'True',
BOTH conditions in it must be 'true'.

Using the example of a course with an exam and an assignment explained above, we can test to see whether a student has passed the unit.
Let's imagine that the pass marks for both components is 40.  We ask the lecturer to type in the marks for both the exam and the assignment, calling one variable ExamMark and the other AssignmentMark. If,
and only if, BOTH marks are 40 or over has the student passed the unit.

We use brackets around each condition and put the AND statement in the centre:

If (ExamMark >= 40) AND (AssignmentMark >= 40) Then
     Form1.Show
     Form1.Print "You have passed the unit"
Else
     Form1.Show
     Form1.Print "You have not passed the unit"
End If


Only if a student has passed the exam
AND the assignment do they pass the whole unit

Task 1

Write a program which reports whether a student has passed a GCSE English exam. The exam consists of 3 components - a written exam, a piece of coursework and a speaking and listening exam. The student must score a mark of 50 in ALL 3 tests to pass the course.

The algorithm should work as follows:

- The program asks the user for the 3 marks via input boxes
- The program reports whether the student has passed or not



OR Conditions

An OR statement is true if
ANY ONE of its conditions is true.
Imagine that tickets to a concert cost ten pounds for adults, but other age groups have a reduced price:

Adult ticket = �10
Junior ticket (for 16's and under) = �5
Senior ticket (for 65's and over) = �5

In this scenario, ticket buyers get a reduction in price:
If they are 16 years old or under
OR 65 years old or older

We ask the user to type in the age of the buyer and then use the following code:

If (Age <= 16) OR (Age >= 65) Then
    Form1.Show
    Form1.Print "Ticket price is �5"
Else
    Form1.Show
    Form1.Print "Ticket price is �10"
End If


Task 2

Write a program which will calculate the sale price of CDs.
If the original price is less than �5 or more than �15 - the discount is 20%
Otherwise, the discount is 30%

The algorithm should work as follows:

- The user is asked to enter the original price, via an input box
- The new price is displayed on a form

Hint:
To give a discount of 20%, multiply the original price by 0.8 (to leave 80% of the original price).
How will you calculate a 30% discount?


End of Tasks

Hosted by www.Geocities.ws

1