1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Working with Intrinsic Controls and ActiveX Controls
30/04/ Selection Nested If structures & Complex Multiple Conditions.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Text Box controls are used when users are required to type some input (during program execution), or output is displayed on the form (known as the user-
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic Chapter 1 Mr. Wangler.
05/09/ Introducing Visual Basic Sequence Programming.
08/09/ Arrays Defining, Declaring & Processing.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
30/10/ Iteration Loops Do While (condition is true) … Loop.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
22/11/ Selection If selection construct.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Controls Part 2. DateTimePicker Control Used for representing Date/Time information and take it as input from user. Date information is automatically.
1 Chapter 4 – Decisions 4.1 Relational and Logical Operators (see other set of slides) 4.2 If Blocks (see other set of slides) 4.3 Select Case Blocks (see.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
31/01/ Selection If selection construct.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Adding Code to the Option Button. Open VB 1.Double click the Calculate button and select General from the Object list box. 2.Add the following code to.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
21/03/ Working with Controls Text and List Boxes.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Visual Basic Fundamental Concepts
Visual Basic.NET Windows Programming
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Microsoft Visual Basic 2005 BASICS
Visual Basic..
Conditions and Ifs BIS1523 – Lecture 8.
المحاضرة السادسة.
If selection construct
Do While (condition is true) … Loop
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
If selection construct
Do … Loop Until (condition is true)
Text / Serial / Sequential Files
3.1 Iteration Loops For … To … Next 18/01/2019.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Review of Previous Lesson
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Group Boxes, Radio buttons and Checked List Boxes
Text / Serial / Sequential Files
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

1 4.2 Selection Logical Operators

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 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 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 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 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 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 Testing check boxes (control).Checked = True (control).Checked = False

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 Program 4.2 Selecting cutlery Create a new project named ‘Selecting cutlery’. Change the form’s Text property to ‘Selecting cutlery’.

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 Control names ControlName ListBoxlstBrands CheckBoxes chkKnife chkFork chkSpoon chkFullSetControlNameRadioButtonsradOne radTwo radFour radEight ButtonbutPrice

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 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 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 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 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 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 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. us/library/139z2azd(v=vs.90).aspxTitle

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 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 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 Program 4.2 Selecting cutlery Test each of the four routes through the code. Save and run the program. Publish the program.

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.

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.

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.

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.

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..

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 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 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.