# 30/04/20151 4.3 Selection Nested If structures & Complex Multiple Conditions.

## Presentation on theme: "30/04/20151 4.3 Selection Nested If structures & Complex Multiple Conditions."— Presentation transcript:

30/04/20151 4.3 Selection Nested If structures & Complex Multiple Conditions

2 Learning Objectives State what is needed for each If statement in Nested If Structures. State which has precedence: And / Or Boolean statements and note that this can cause problems in complex multiple conditions.

3 Nested If Structures An alternative to using multiple And conditions.

4 Multiple And Boolean Statement So instead of using a multiple And condition like:

5 Multiple 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

6 Nested If Structure We could use:

7 Nested If Structure Dim Age As Integer Dim Gender As String Age = txtAge.Text Gender = txtGender.Text If Age >= 18 Then ‘Aged 18 or over? If Gender = “F” Then ‘and female? If Gender = “F” Then ‘and female? lblMessage.Text = “Allow into nightclub.” Else ‘All other people. Else ‘All other people. lblMessage.Text = “Do not allow into nightclub!” End If End If End If 12

8 Nested If Structures We can also replace ElseIf structures with nested If structures.

9 ElseIf structures So instead of:

10 ElseIf structures Dim Mark As Integer Mark = InputBox(“Enter an exam mark from 0 to 100.”) If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) MsgBox (“Merit”) ElseIf Mark >= 40 Then ‘Mark 40 - 59? MsgBox (“Pass”) MsgBox (“Pass”) Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) MsgBox (“A mark of “ & Mark & “ is a fail.”) End If

11 Nested If Structures We can use:

12 Nested If Structures If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) MsgBox (“Merit”)Else If Mark >= 40 Then ‘Mark 40 - 59? If Mark >= 40 Then ‘Mark 40 - 59? MsgBox (“Pass”) Else ‘Mark under 40. Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) End If End If End If 1 2

13 Nested If Structures Note: If you use nested If structures you need to remember to use an End If statement for each If statement. If you use nested If structures you need to remember to use an End If statement for each If statement.

14 Program 4.3 Rent a property Specification: Illustrate complex multiple conditions: Illustrate complex multiple conditions: More specifically to illustrate the order of precedence of the And & Or logical operators. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house.

15 Program 4.3 Rent a property Create a new project named ‘Rent a property’. Change the form’s Text property to ‘Rent a property’.

16 The Form Two labels. Two list boxes. One button. Set their text / item properties as shown on the form opposite.

17 Program 4.3 Rent a property Name the list boxes lstTypes and lstBedrooms as appropriate. Name the button butRent.

18 Program 4.3 Rent a property butRent code: If (lstTypes.Text = "Cottage") Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then If (lstTypes.Text = "Cottage") Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then MsgBox("Rent it!") Else Else MsgBox("Don’t rent it!") End If End If

19 Program 4.3 Rent a property Run the program and test all possibilities. You should find a problem! What is it?

20 Program 4.3 Rent a property You should have found that if it is a cottage and the number of bedrooms is lower than 4 the program still says ‘Rent it!’.

21 Program 4.3 Rent a property This is because And is done before Or. So the program interprets the code like this: (lstTypes.Text = "Cottage") (lstTypes.Text = "Cottage") Or Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) So if it is Detached then this works as both the type and the number of bedrooms has to be true but if it is a cottage then the type or the number of bedrooms is required to be true; so it doesn’t work.

22 Program 4.3 Rent a property Replace the Rent button’s template code with: If (lstTypes.Text = "Cottage") And (lstBedrooms.Text >= 4) Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then If (lstTypes.Text = "Cottage") And (lstBedrooms.Text >= 4) Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then MsgBox("Rent it!") Else Else MsgBox("Don’t rent it!") End If End If

23 Program 4.3 Rent a property Run the program and test all possibilities. It should work perfectly now.

24 Extension “Work Hours” Program 1 For each employee the hours worked module collects data for five days. If the user leaves a day empty give a message box asking the user to enter a 0. Use one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. Each person can work up to 9 hours a day for up to 5 days a week. Use one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. No-one may work more than 40 hours. If all the above requirements are met then the hours are added up and displayed. I suggest 5 text boxes (one for each day), a button and a label to display the Total Number of Hours worked. I suggest 5 text boxes (one for each day), a button and a label to display the Total Number of Hours worked.

2530/04/2015 Concatenation contents of the Name variable contents of the Age variable Note the spaces needed to create a readable message. Join variables and strings together using the & operator. e.g. Putting a variable in a message: e.g. Putting a variable in a message: MsgBox (Name & “, you are “ & Age & “ years old.”) MsgBox (Name & “, you are “ & Age & “ years old.”) Will show: Will show: ……, you are … years old.

26 Extension “Vehicle Type” Program 2 Vehicle Type: Enter See the next slide for more details.

27 Extension “Vehicle Type” Program 2 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 one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. 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 previous slides. concatenation

30 Extension “Winner or Winners” Program 4 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.

31 Extension “Mouse Clicks” Program 5 Extend the “Global Variables/Mouse Clicks” program: 3.2 Working with Data 3.2 Working with Data 3.2 Working with Data 3.2 Working with Data Rename the original button as “Button A” and add two similar buttons “Button B” and “Button C”. The program should record how many times each button is clicked but should no longer display these numbers on the screen as they are clicked. Instead, add an extra “Winner” button which, when clicked, displays, the name of the button which was clicked the most times (i.e. the “Winner”). Also either: Add a “Reset” button. Add a “Reset” button.Or Declare the “The winner/s was/were” and then reset automatically. Declare the “The winner/s was/were” and then reset automatically. Do not use “Application.Reset” as exams will not allow you to use this “simplistic” way. Please use the more “manual complex” way. Do not use “Application.Reset” as exams will not allow you to use this “simplistic” way. Please use the more “manual complex” way.

32 Plenary What is needed for each If statement in Nested If Structures? End If End If Which has precedence: And / Or? And And

Download ppt "30/04/20151 4.3 Selection Nested If structures & Complex Multiple Conditions."

Similar presentations