1 2.2 Selection Logical Operators
2 Learning Objectives Explain how the logical operator AND Boolean statements works.
3 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.
4 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.
5 Logical Operators (Boolean Statements) The two main ones are: And 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. Or 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. Ands have precedence over Ors Ands have precedence over Ors
6 Program 2.2a Night Club Specification: A program to test conditions for customers waiting to enter into a club holding a ladies only night: A program to test conditions for customers waiting to enter into a club holding a ladies only night: Age >= 18 Gender = “F” Each of these is either true or false. Each of these is either true or false. Anybody else should be refused entry with a simple general message: Anybody else should be refused entry with a simple general message: “Do not allow into nightclub!”.
7 Program 2.2a Night Club Dim Age As Integer Dim Gender As String Console.WriteLine(“Please enter your age.”) Age = Console.ReadLine Console.WriteLine(“Please enter your gender (M/F).”) Gender = Console.ReadLine If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? Console.WriteLine (“Allow into nightclub.”) Console.WriteLine (“Allow into nightclub.”) Else ‘ Everybody else Console.WriteLine(“Do not allow into nightclub!”) Console.WriteLine(“Do not allow into nightclub!”) End If
8 Program 2.2b 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.
9 Or Boolean Statement Dim TwoForty As Integer ‘ Dim TwoHundred As Integer ‘Note that VB will not allow numbers as identifiers. Console.WriteLine(“How many times have you scored 240 points?”) TwoForty = Console.ReadLine Console.WriteLine(“How many times have you scored 200 points?”) TwoHundred = Console.ReadLine If (TwoForty >= 5) Or (TwoHundred >= 10) Then ‘Scored at least 240 points on 5 or more occasions, or 200 points on 10 or more occasions? Console.WriteLine (“Give award.”) Console.WriteLine (“Give award.”) End If
Logical Expressions If an exam asks for “a logical expression” then this means a “single logical expression”. Single Logical Expression: Single Logical Expression: If … = … And … = … Then If … = … Or … = … Then If combination of And’s, Or’s, () Then Basically one If statement with “And” &/ “Or ”. Basically one If statement with “And” &/ “Or ”. NOT nested If statements. 10
1116/10/2015 Extension “Hotel” Program 2.2c Write a program for a person wishing to attend an overnight conference: They cannot afford to pay more than €40.00 for their hotel but it must be no more than 3km from the conference hall. They cannot afford to pay more than €40.00 for their hotel but it must be no more than 3km from the conference hall. Use a Logical Expression. 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. 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. Extension: 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.
12 Extension “Salary” Program 2.2d Extend the “Salary” Program written in 1 Variables/Identifiers. 1 Variables/Identifiers1 Variables/Identifiers Limit the user so that they cannot work more than 40 hours. Limit the user so that they cannot work more than 40 hours. Includes normal hours and overtime hours. Use a logical expression. Use a logical expression. Note: 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.2d).
13 Extension “Deciding Exam Grades” Program 2.2e. Change the “Deciding Exam Grades” Program written in 2.1 Selection. 2.1 Selection2.1 Selection To give a general error message. To give a general error message. “You have entered an invalid mark!” Deciding Exam Grades 2.2e: Deciding Exam Grades 2.2e: 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. Note: 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 (FalseIfValid Version 2.2e).
14 Extension “Vehicle Type” Program 2.2f 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. Invalid vehicle types should produce the error message: Invalid vehicle types should produce the error message: “Invalid”. Use a logical expression. Use a logical expression. Valid vehicle types should produce a message: Valid vehicle types should produce a message: ….. is valid ….. is valid 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. vehicle type Hint: Use concatenation – see presentation 2.1. concatenation2.1concatenation2.1
15 Extension “Work Hours” Program 2.2g 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. Use a logical expression. 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.
16 Extension “Winner or Winners” Program 2.2h 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). Extension: Extension: Adapt the program to deal correctly with three or two of the candidates receiving equal marks. Hints: 1. Test for clear winners first, then for three winners and then for two winners. 2. Either use: A series of ElseIf statements. A series of ElseIf statements.Or Separate If statements with Exit Sub after each winner is declared 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 the program.
17 Plenary Explain how the logical operator AND works. When you And two or more conditions each one must be true for the overall condition to be true. 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. If just one of them is false the overall condition is false.