2 3 Selection Nested If structures Complex Multiple






















- Slides: 22
2. 3 Selection Nested If structures & Complex Multiple Conditions 2/15/2022 1
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. 2
Nested If Structures An alternative to using multiple And conditions. 3
Multiple And Boolean Statement So instead of using a multiple And condition like: 4
Multiple And Boolean Statement Dim Age As Integer Dim Gender As String Age = Console. Read. Line Gender = Console. Read. Line If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? n Console. Write. Line(“Allow into nightclub. ”) Else ‘ Everybody else n Console. Write. Line(“Do not allow into nightclub. ”) End If 5
Nested If Structure We could use: 6
Nested If Structure Dim Age As Integer Dim Gender As String Age = Console. Read. Line Gender = Console. Read. Line If Age >= 18 Then ‘Aged 18 or over? n 1 2 If Gender = “F” Then ‘and female? Console. Write. Line(“Allow into nightclub. ”) n Else ‘All other people. Console. Write. Line(“Do not allow into nightclub!”) n End If 7
Nested If Structures We can also replace Else. If structures with nested If structures. 8
Else. If structures So instead of: 9
Else. If structures Dim Mark As Integer Mark = Console. Read. Line If Mark >=60 Then ‘Mark 60 or more? n Console. Write. Line(“Merit”) Else. If Mark >= 40 Then ‘Mark 40 - 59? n Console. Write. Line(“Pass”) Else ‘Mark under 40. n Console. Write. Line(“A mark of “ & Mark & “ is a fail. ”) End If 10
Nested If Structures We can use: 11
Nested If Structures If Mark >=60 Then ‘Mark 60 or more? n Console. Write. Line(“Merit”) Else n 1 2 If Mark >= 40 Then ‘Mark 40 - 59? Console. Write. Line(“Pass”) n Else ‘Mark under 40. Console. Write. Line(“A mark of “ & Mark & “ is a fail. ”) n End If 12
Nested If Structures Note: n If you use nested If structures you need to remember to use an End If statement for each If statement. 13
Program 2. 3 a Rent a property Specification: n Illustrate complex multiple conditions: More specifically to illustrate the order of precedence of the And & Or logical operators. n A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house. 14
Program 2. 3 a Rent a property Dim Type As String Dim Bedrooms As Integer Console. Write. Line(“Please enter the property type. ”) Type = Console. Read. Line Console. Write. Line(“Please enter the number of bedrooms. ”) Bedrooms = Console. Read. Line If Type = "Cottage" Or Type = "Detached" And Bedrooms >= 4 Then n Console. Write. Line("Rent it!") Else n Console. Write. Line("Don’t rent it!") End If 15
Program 2. 3 a Rent a property Run the program and test all possibilities. You should find a problem! What is it? 16
Program 2. 3 a 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!’. 17
Program 2. 3 a Rent a property This is because And is done before Or. So the program interprets the code like this: n Type = "Cottage" n Or n Type = "Detached" And Bedrooms >= 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. 18
Program 2. 3 a Rent a property Replace the previous code with: n n n If (Type = "Cottage" And Bedrooms >= 4) Or (Type = "Detached" And Bedrooms >= 4) Then Console. Write. Line("Rent it!") Else Console. Write. Line("Don’t rent it!") End If 19
Program 2. 3 a Rent a property Run the program and test all possibilities. It should work perfectly now. 20
Extension “Deciding Exam Grades” Program 2. 3 b Change the “Deciding Exam Grades” Program written in 2. 1 / 2. 2 Selection. n Use a logical expression that is true when the mark is within the range and false when the mark is outside the range of 0 – 100 and. Therefore use one IF statement with an initial IF test with And, to test if the mark is valid proceeded with a nested IF statement to produce the appropriate valid messages: Merit (60 or more), Pass (40 – 59), Fail (under 40). n With a final “Else” to produce the general error message: “You have entered an invalid mark!” n 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 (Nested If 21 Version 2. 3 b).
Plenary What is needed for each If statement in Nested If Structures? n End If Which has precedence: And / Or? n And 22