1 2.2 selection logical operators. 2 learning objectives explain how the logical operator and...

17
1 2.2 Selection 2.2 Selection Logical Operators Logical Operators

Upload: myrtle-manning

Post on 01-Jan-2016

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

11

2.2 Selection2.2 Selection

Logical OperatorsLogical Operators

Page 2: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

22

Learning ObjectivesLearning Objectives

Explain how the logical operator Explain how the logical operator ANDAND Boolean statements works.Boolean statements works.

Page 3: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

33

Using brackets in If structuresUsing brackets in If structures

From this point we will use brackets in From this point we will use brackets in IfIf structures.structures.

You do not have to use them but they You do not have to use them but they make complex make complex IfIf structures more structures more readable.readable.

State how to set an initial value of a State how to set an initial value of a variable when it is declared.variable when it is declared.

Page 4: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

44

Testing multiple Boolean Statement Testing multiple Boolean Statement conditionsconditions

The boolean condition has so far The boolean condition has so far consisted of one test.consisted of one test.

A multiple boolean condition has two or A multiple boolean condition has two or more tests and each one is either true or more tests and each one is either true or false.false.

For this you need to use VB’s logical For this you need to use VB’s logical operators.operators.

Page 5: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

55

Logical Operators Logical Operators (Boolean Statements)(Boolean Statements)

The two main ones are:The two main ones are: AndAnd

When you When you AndAnd two or more conditions each one two or more conditions each one must be true for the overall condition to be true.must be true for the overall condition to be true.If just one of them is false the overall condition is If just one of them is false the overall condition is false.false.

OrOrWhen you When you OrOr two or more conditions, then if at two or more conditions, then if at least one of them is true the overall condition is least one of them is true the overall condition is true.true.They must all be false for the overall condition to They must all be false for the overall condition to be false.be false.

AndAnds have precedence over s have precedence over OrOrss

Page 6: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

66

Program 2.2a Night ClubProgram 2.2a Night Club

SpecificationSpecification:: A program to test conditions for customers A program to test conditions for customers

waiting to enter into a club holding a ladies waiting to enter into a club holding a ladies only night:only night:

Age >= 18Age >= 18

Gender = “F”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 Anybody else should be refused entry with a simple general message:simple general message:

““Do not allow into nightclub!”.Do not allow into nightclub!”.

Page 7: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

77

Program 2.2a Night ClubProgram 2.2a Night ClubDim Age As IntegerDim Age As IntegerDim Gender As StringDim Gender As StringConsole.WriteLine(“Please enter your age.”)Console.WriteLine(“Please enter your age.”)Age = Console.ReadLineAge = Console.ReadLineConsole.WriteLine(“Please enter your gender (M/F).”)Console.WriteLine(“Please enter your gender (M/F).”)Gender = Console.ReadLineGender = Console.ReadLineIf (Age >= 18) And (Gender = “F”) Then If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and ‘ Female ‘ 18 and over?over? Console.WriteLineConsole.WriteLine(“Allow into nightclub.”)(“Allow into nightclub.”)

Else Else ‘ Everybody else‘ Everybody else Console.WriteLine(“Do not allow into nightclub!”)Console.WriteLine(“Do not allow into nightclub!”)

End IfEnd If

Page 8: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

88

Program 2.2b Bowling ClubProgram 2.2b Bowling Club

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

Page 9: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

99

Or Boolean StatementOr Boolean StatementDim TwoForty As Integer Dim TwoForty As Integer ‘ ‘ Dim TwoHundred As Integer Dim TwoHundred As Integer ‘‘Note that VB will not allow numbers as identifiers.Note that VB will not allow numbers as identifiers.Console.WriteLine(“How many times have you scored 240 Console.WriteLine(“How many times have you scored 240 points?”)points?”)TwoForty = Console.ReadLineTwoForty = Console.ReadLineConsole.WriteLine(“How many times have you scored 200 Console.WriteLine(“How many times have you scored 200 points?”)points?”)TwoHundred = Console.ReadLineTwoHundred = Console.ReadLineIf (TwoForty >= 5) Or (TwoHundred >= 10) ThenIf (TwoForty >= 5) Or (TwoHundred >= 10) Then‘‘Scored at least 240 points on 5 or more occasions, or 200 Scored at least 240 points on 5 or more occasions, or 200 points on 10 or more occasions?points on 10 or more occasions? Console.WriteLineConsole.WriteLine(“Give award.”)(“Give award.”)

End IfEnd If

Page 10: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

Logical ExpressionsLogical Expressions

If an exam asks for “a logical expression” then If an exam asks for “a logical expression” then this means a “this means a “singlesingle logical expression”. logical expression”. Single Logical ExpressionSingle Logical Expression::

If … = … And … = … ThenIf … = … And … = … Then

If … = … Or … = … ThenIf … = … Or … = … Then

If If combination of And’s, Or’s, () combination of And’s, Or’s, () ThenThen Basically Basically one If statement one If statement with “with “AndAnd” &/ “” &/ “OrOr”.”.

NOTNOT nested If statements. nested If statements.

1010

Page 11: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

111104/20/2304/20/23

Extension “Hotel” Program 2.2cExtension “Hotel” Program 2.2c

Write a program for a person wishing to attend an Write a program for a person wishing to attend an overnight conference:overnight conference: They cannot afford to pay more than €40.00 for their They cannot afford to pay more than €40.00 for their

hotel but it must be no more than 3km from the hotel but it must be no more than 3km from the conference hall.conference hall.

Use a Logical Expression . The program should ask for the cost per night and The program should ask for the cost per night and

distance from the conference hall and then display a distance from the conference hall and then display a message stating whether the booking should be made message stating whether the booking should be made or not.or not.

Use a Logical Expression . Extension:Extension:

Give appropriate messages if the distance and the cost are not Give appropriate messages if the distance and the cost are not good (and what they should be), if the distance is good but the good (and what they should be), if the distance is good but the price is not (and what it should be) and vice versa.price is not (and what it should be) and vice versa.

Page 12: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1212

Extension “Salary” Program 2.2dExtension “Salary” Program 2.2d

Extend the “Salary” Program written in 1 Extend the “Salary” Program written in 1 Variables/IdentifiersVariables/Identifiers..

Limit the user so that they cannot Limit the user so that they cannot work more than 40 work more than 40 hours. hours.

Includes normal hours Includes normal hours andand overtime hours. overtime hours. Use a logical expression.Use a logical expression. Note:Note:

Make a copy of the previous program’s whole folder to Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the keep the original program but rename this folder with the same name but add (same name but add (Total Hours Restricted Version Total Hours Restricted Version 2.2d2.2d).).

Page 13: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1313

Extension “Deciding Exam Grades” Extension “Deciding Exam Grades” Program 2.2e.Program 2.2e.

Change the “Deciding Exam Grades” Program Change the “Deciding Exam Grades” Program written in written in 2.1 Selection.. To give a general error message.To give a general error message.

““You have entered an invalid mark!”You have entered an invalid mark!”

Deciding Exam Grades 2.2Deciding Exam Grades 2.2ee::Use a logical expression that is Use a logical expression that is false when the mark is false when the mark is within the rangewithin the range and true when the mark is outside the and true when the mark is outside the range of 0 – 100 and range of 0 – 100 and use use OrOr to produce the general error to produce the general error message above.message above.

Note:Note:Make a copy of the previous program’s whole folder to Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the keep the original program but rename this folder with the same name but add (same name but add (FalseIfValid Version 2.2eFalseIfValid Version 2.2e).).

Page 14: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1414

Extension “Vehicle Type” Program Extension “Vehicle Type” Program 2.2f2.2f

Write a program to allow only car, motorbike and Write a program to allow only car, motorbike and lorry as valid vehicle types.lorry as valid vehicle types.

Note that you must declare a variable to store the vehicle type so 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 this will be the “first” time you will need to declare a variable as “String” as it will not be a number.“String” as it will not be a number.

Invalid vehicle types should produce the error message: Invalid vehicle types should produce the error message: ““InvalidInvalid”.”.

Use a logical expression.Use a logical expression.

Valid vehicle types should produce a message:Valid vehicle types should produce a message: …….... is validis valid

Does the program also accept Car, Does the program also accept Car, Motorbike and Lorry?Motorbike and Lorry?

Find out but do not attempt to change this.Find out but do not attempt to change this.

Please explain what happens and why in Please explain what happens and why in your comments.your comments.

vehicle typevehicle type

Hint: Use concatenation – see presentation 2.1.Hint: Use concatenation – see presentation 2.1.

Page 15: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1515

Extension “Work Hours” Extension “Work Hours” Program 2.2gProgram 2.2g

For each employee the hours worked module For each employee the hours worked module collects data for five days. collects data for five days. Each person can work up to 9 hours a day for up Each person can work up to 9 hours a day for up to 5 days a week. to 5 days a week. Use a logical expression.Use a logical expression.

No-one may work more than 40 hours.No-one may work more than 40 hours.If all the above requirements are met then the If all the above requirements are met then the hours are added up and displayed.hours are added up and displayed.

Page 16: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1616

Extension “Winner or Winners” Extension “Winner or Winners” Program 2.2hProgram 2.2h

Write a program to accept three marks for three Write a program to accept three marks for three candidates candidates Candidate ACandidate A, , Candidate BCandidate B and and Candidate CCandidate C..The program should display the winner The program should display the winner (highest mark).(highest mark).

Extension:Extension:Adapt the program to deal correctly with three or two of the Adapt the program to deal correctly with three or two of the candidates receiving equal marks.candidates receiving equal marks.

Hints:Hints: 1.1. Test for clear winners first, then for three winners and then for two Test for clear winners first, then for three winners and then for two

winners.winners.2.2. Either use:Either use:

A series of A series of ElseIfElseIf statements. statements.

OrOr Separate If statements with Separate If statements with Exit SubExit Sub after each winner is declared after each winner is declared

So as to help make other IF’s less complicated or run the chance 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 of a correct winner being replaced by incorrect winners later on in the program.the program.

Page 17: 1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works

1717

PlenaryPlenary

Explain how the logical operator Explain how the logical operator ANDAND works.works. When you When you AndAnd two or more conditions each two or more conditions each

one must be true for the overall condition to one must be true for the overall condition to be true.be true.

If just one of them is false the overall If just one of them is false the overall condition is false.condition is false.