Download presentation
Presentation is loading. Please wait.
Published byMyron Lawrence Modified over 8 years ago
1
1 4.2 Selection Logical Operators
2
2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes are empty to avoid crashes when the data of one type is attempted to be stored in variables of another type e.g. string data in an integer variable. Setting an initial value of a variable when it is declared.
3
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
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
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
6 And Boolean Statement Example We want to test two conditions for customers waiting to enter into a club holding a ladies only night: Age >= 18 Age >= 18 Gender = “F” Gender = “F” Each of these is either true or false.
7
7 And Boolean Statement Dim Age As Integer Dim Gender As String Age = txtAge.Text Gender = txtGender.Text If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? MsgBox (“Allow into nightclub.”) MsgBox (“Allow into nightclub.”) Else ‘ Everybody else MsgBox (“Do not allow into nightclub.”) MsgBox (“Do not allow into nightclub.”) End If
8
8 Or Boolean Statement Example 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
9 Or Boolean Statement Dim TwoForty As Integer ‘ Note that VB will not allow Dim TwoHundred As Integer ‘ numbers as identifiers. ‘ Assume some form of data input. If (TwoForty >= 5) Or (TwoHundred >= 10) Then MsgBox (“Give award.”) MsgBox (“Give award.”) End If
10
10 Testing check boxes (control).Checked = True (control).Checked = False
11
11 Program 4.2 Selecting cutlery Specification: A program for a mail order cutlery company. A program for a mail order cutlery company. The form should have a list of cutlery brands, a list of different cutlery items and a list of purchase quantities. The form should have a list of cutlery brands, a list of different cutlery items and a list of purchase quantities. The user must select at least one item from each of the three lists and then click a button for the price to be calculated. The user must select at least one item from each of the three lists and then click a button for the price to be calculated. N.B. The cost calculation will not actually be done as it is not relevant to the concept of multiple conditions being learned here. N.B. The cost calculation will not actually be done as it is not relevant to the concept of multiple conditions being learned here.
12
12 Program 4.2 Selecting cutlery Create a new project named ‘Selecting cutlery’. Change the form’s Text property to ‘Selecting cutlery’.
13
13 The Form One Label 1 ListBox 2 GroupBoxes 1 with 3 CheckBoxes inside 1 with 3 CheckBoxes inside the other with 4 RadioButtons inside the other with 4 RadioButtons inside 1 CheckBox on its own 1 Button
14
14 Control names ControlName ListBoxlstBrands CheckBoxes chkKnife chkFork chkSpoon chkFullSetControlNameRadioButtonsradOne radTwo radFour radEight ButtonbutPrice
15
15 Program 4.2 Selecting cutlery When the Full Set CheckBox is checked the check boxes in the Items GroupBox must be selected automatically.
16
16 Program 4.2 Selecting cutlery Double click the Full Set CheckBox and note the default event is CheckedChanged. Click the events list and note that there is no Checked event.
17
17 Program 4.2 Selecting cutlery So the code you enter here: Will be executed if the user checks or un- checks the Full Set CheckBox. Will be executed if the user checks or un- checks the Full Set CheckBox. So the code must check if the Full Set CheckBox has been checked or un-checked before deciding what to do. So the code must check if the Full Set CheckBox has been checked or un-checked before deciding what to do.
18
18 Program 4.2 Selecting cutlery Double click the Full Set CheckBox and enter the following code in its template: ‘Full Set checked? ‘Full Set checked? If chkFullSet.Checked = True Then If chkFullSet.Checked = True Then ‘Select all 3 check boxes. chkKnife.Checked = True chkFork.Checked = True chkSpoon.Checked = True Else ‘Full Set not selected. Else ‘Full Set not selected. ‘Deselect all 3 check boxes. chkKnife.Checked = False chkFork.Checked = False chkSpoon.Checked = False End If End If
19
19 Program 4.2 Selecting cutlery When the Price button is clicked each list should be checked to see if the user has selected at least one item from each and then one of the following messages should be displayed: You must select a brand! You must select a brand! You must select one or more items! You must select one or more items! You must select a quantity! You must select a quantity! All three things have been selected! All three things have been selected!
20
20 Program 4.2 Selecting cutlery There needs to be 4 routes through the Price button’s code (i.e. one for each possible message). Two ElseIfs and an Else can do this.
21
21 Message Boxes You can also give a message box a title by using MsgBox(“……… ",, “……….") Message MsgBoxStyle enumeration value If left empty the MsgBox will just have an OK option. See the link below for other possible values, if you wish to investigate. http://msdn.microsoft.com/en- us/library/139z2azd(v=vs.90).aspxTitle
22
22 Program 4.2 Selecting cutlery Note: With the Brands list we only need to test its text property to see if anything has been selected. With the Brands list we only need to test its text property to see if anything has been selected. With the other two lists we need to check if each check box or radio button has been selected. With the other two lists we need to check if each check box or radio button has been selected. Therefore we need Ands.
23
23 Program 4.2 Selecting cutlery Price button code: If lstBrands.Text = "" Then 'Has a brand not been selected? If lstBrands.Text = "" Then 'Has a brand not been selected? MsgBox("You must select a brand!",, "Brand") ' Brand has been selected but has an item been selected? ' Brand has been selected but has an item been selected? ElseIf (chkKnife.Checked = False) And chkFork.Checked = False) And (chkSpoon.Checked = False) Then ElseIf (chkKnife.Checked = False) And chkFork.Checked = False) And (chkSpoon.Checked = False) Then MsgBox("You must select one or more items!",, "Items") Continued on next slide:
24
24 Program 4.2 Selecting cutlery 'Brand and Item have been selected but has a quantity? 'Brand and Item have been selected but has a quantity? ElseIf (radOne.Checked = False) And (radTwo.Checked = False) And (radFour.Checked = False) And (radEight.Checked = False) Then ElseIf (radOne.Checked = False) And (radTwo.Checked = False) And (radFour.Checked = False) And (radEight.Checked = False) Then MsgBox("You must select a quantity!",, "Quantity") Else 'Everything has been selected. Else 'Everything has been selected. MsgBox("All three things have been selected!",, "Cutlery") End If End If
25
25 Program 4.2 Selecting cutlery Test each of the four routes through the code. Save and run the program. Publish the program.
26
26 Extensions 1 Although checking the Full Set check box selects the other 3 check boxes, if you then uncheck one of these three the Full Set check box remains selected. Add code to ensure that the Full Set check box is only selected when the other 3 are selected.
27
2720/03/2016 Extension “Hotel” Program 3 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. AndOr Use one IF and decide whether to use And or Or. 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. AndOr Use one IF and decide whether to use And or Or. 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.
28
Extension 4: Average – Highest & Lowest Extend the “Average Mean” Program written in 3.3 Working with Data to also: 3.3 Working with Data Only allow the user to enter marks from 0 to 100. AndOr Use one IF and decide whether to use And or Or. Use Message boxes to display “Failed” if a mark is less than 40 and “Merit” if a mark is above 60. Display on the form the number of merit marks. Display the highest mark and the lowest marks. Hint: Declare two global variables Lowest and Highest. Further hints for 2 possible solutions are on the next 2 slides.
29
2920/03/2016 Setting an initial value of a variable when it is declared. Dim … As … = … Can be used to set an initial value of a variable when it is declared. Can be used to set an initial value of a variable when it is declared.
30
Extension 4: Average – Highest & Lowest Possible Solution 1: As the marks have to be from 0 to 100 we can use these borderline values as the initial highest and lowest values respectively by setting: Dim Lowest As Integer = 100 Dim Highest As Integer = 0 Make sure you include an explanation of these lines in your comments. Each time a mark is entered, compare the newly entered mark with the variable Lowest (NewMark<Lowest). If it is lower then change the Lowest mark. If NewMark < Lowest Then Lowest = NewMark Do the same for Highest but in reverse. Remember to display the Lowest and Highest in appropriate labels. When you comment on your If statements make sure you explain where you are testing and why does it have to be here, what you are testing for, why are you testing for this and what happens if the test is true. Remember to edit the “Reset” button as well. A second possible solution is on the next slide, I will leave you to decide which one you choose to actually use..
31
Extension 4: Average – Highest & Lowest Possible Solution 2: The first mark entered (butOK) could be stored in both the Lowest & Highest variables. If NumberOfMarks = 1 Then Lowest = Highest = NewMark You will then need to either position this new code at a certain place in the existing code or move the line which updates the NumberOfMarks (NumberOfMarks = NumberOfMarks + 1) to before you test for the first mark above or change how you test for the first mark. Each time a mark is entered, compare the newly entered mark with the variable Lowest (NewMark<Lowest). If it is lower then change the Lowest mark. If NewMark < Lowest Then Lowest = NewMark Do the same for Highest but in reverse. Remember to display the Lowest and Highest in appropriate labels. When you comment on your If statements make sure you explain where you are testing and why does it have to be here, what you are testing for, why are you testing for this and what happens if the test is true. Remember to edit the “Reset” button as well. I will leave you to decide which solution you choose to actually use.
32
32 Extension “Salary” Program 5 Extend the “Salary” Program written in 3.1 Working with Data 3.1 Working with Data3.1 Working with Data At the moment the program crashes if you try to store the overtime hours when the overtime text box is empty “”. To stop this, use an If statement to test if the text box is NOT <> empty “” before attempting to store the overtime hours e.g. To stop this, use an If statement to test if the text box is NOT <> empty “” before attempting to store the overtime hours e.g. ‘If the overtime text box is NOT <> empty “” then store its contents, otherwise leave the variable as 0 from when it was declared. If txtOvertimeHours.Text <> “” Then OvertimeHours = txtOvertimeHours.Text OvertimeHours = txtOvertimeHours.Text End If Limit the user so that they cannot work more than 40 hours. Includes normal hours and overtime hours. Includes normal hours and overtime hours.
33
33 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.