2 2 Selection Logical Operators 1 Learning Objectives

2. 2 Selection Logical Operators 1

Learning Objectives Explain how the logical operator AND Boolean statements works. 2

Using brackets in If structures From this point we will use brackets in If structures. You do not have to use them but they make complex If structures more readable. State how to set an initial value of a variable when it is declared. 3

Testing multiple Boolean Statement conditions The boolean condition has so far consisted of one test. A multiple boolean condition has two or more tests and each one is either true or false. For this you need to use VB’s logical operators. 4

Logical Operators (Boolean Statements) The two main ones are: n And When you And two or more conditions each one must be true for the overall condition to be true. If just one of them is false the overall condition is false. n Or When you Or two or more conditions, then if at least one of them is true the overall condition is true. They must all be false for the overall condition to be false. n Ands have precedence over Ors 5

Program 2. 2 a Night Club Specification: n A program to test conditions for customers waiting to enter into a club holding a ladies only night: Age >= 18 Gender = “F” n n Each of these is either true or false. Anybody else should be refused entry with a simple general message: “Do not allow into nightclub!”. 6

Program 2. 2 a Night Club Dim Age As Integer Dim Gender As String Console. Write. Line(“Please enter your age. ”) Age = Console. Read. Line Console. Write. Line(“Please enter your gender (M/F). ”) Gender = Console. Read. Line If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? n Console. Write. Line(“Allow into nightclub. ”) Else ‘ Everybody else n Console. Write. Line(“Do not allow into nightclub!”) End If 7

Program 2. 2 b Bowling Club Members of a ten-pin bowling club get an award if, during one season, they score at least 240 points on 5 or more occasions, or they score 200 points on 10 or more occasions. 8

Or Boolean Statement Dim Two. Forty As Integer ‘ Dim Two. Hundred As Integer ‘Note that VB will not allow numbers as identifiers. Console. Write. Line(“How many times have you scored 240 points? ”) Two. Forty = Console. Read. Line Console. Write. Line(“How many times have you scored 200 points? ”) Two. Hundred = Console. Read. Line If (Two. Forty >= 5) Or (Two. Hundred >= 10) Then ‘Scored at least 240 points on 5 or more occasions, or 200 points on 10 or more occasions? n Console. Write. Line(“Give award. ”) End If 9

Logical Expressions If an exam asks for “a logical expression” then this means a “single logical expression”. n Single Logical Expression: If … = … And … = … Then If … = … Or … = … Then If combination of And’s, Or’s, () Then n Basically one If statement with “And” &/ “Or”. NOT nested If statements. 10

Extension “Hotel” Program 2. 2 c Write a program for a person wishing to attend an overnight conference: n They cannot afford to pay more than € 40. 00 for their hotel but it must be no more than 3 km from the conference hall. Use a Logical Expression. n The program should ask for the cost per night and distance from the conference hall and then display a message stating whether the booking should be made or not. Use a Logical Expression. n Extension: Give appropriate messages if the distance and the cost are not good (and what they should be), if the distance is good but the price is not (and what it should be) and vice versa. 1/9/2022 11

Extension “Salary” Program 2. 2 d Extend the “Salary” Program written in 1 Variables/Identifiers. n Limit the user so that they cannot work more than 40 hours. Includes normal hours and overtime hours. n n Use a logical expression. Note: Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Total Hours Restricted Version 2. 2 d). 12

Extension “Deciding Exam Grades” Program 2. 2 e. Change the “Deciding Exam Grades” Program written in 2. 1 Selection. n To give a general error message. “You have entered an invalid mark!” n Deciding Exam Grades 2. 2 e: Use a logical expression that is false when the mark is within the range and true when the mark is outside the range of 0 – 100 and use Or to produce the general error message above. n Note: Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (False. If. Valid Version 2. 2 e). 13

Extension “Vehicle Type” Program 2. 2 f Write a program to allow only car, motorbike and lorry as valid vehicle types. Note that you must declare a variable to store the vehicle type so this will be the “first” time you will need to declare a variable as “String” as it will not be a number. n Invalid vehicle types should produce the error message: “Invalid”. n n Use a logical expression. Valid vehicle types should produce a message: …. . is valid vehicle type Hint: Use concatenation – see presentation 2. 1. Does the program also accept Car, Motorbike and Lorry? Find out but do not attempt to change this. Please explain what happens and why in your comments. 14

Extension “Work Hours” Program 2. 2 g For each employee the hours worked module collects data for five days. Each person can work up to 9 hours a day for up to 5 days a week. n Use a logical expression. No-one may work more than 40 hours. If all the above requirements are met then the hours are added up and displayed. 15

Extension “Winner or Winners” Program 2. 2 h Write a program to accept three marks for three candidates Candidate A, Candidate B and Candidate C. The program should display the winner (highest mark). n Extension: Adapt the program to deal correctly with three or two of the candidates receiving equal marks. Hints: 1. 2. Test for clear winners first, then for three winners and then for two winners. Either use: n A series of Else. If statements. Or n Separate If statements with Exit Sub after each winner is declared So as to help make other IF’s less complicated or run the chance of a correct winner being replaced by incorrect winners later on in 16 the program.

Plenary Explain how the logical operator AND works. n n When you And two or more conditions each one must be true for the overall condition to be true. If just one of them is false the overall condition is false. 17
- Slides: 17