Presentation is loading. Please wait.

Presentation is loading. Please wait.

26/10/20151 2.3 Selection Nested If structures & Complex Multiple Conditions.

Similar presentations


Presentation on theme: "26/10/20151 2.3 Selection Nested If structures & Complex Multiple Conditions."— Presentation transcript:

1 26/10/20151 2.3 Selection Nested If structures & Complex Multiple Conditions

2 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 3 Nested If Structures An alternative to using multiple And conditions.

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

5 5 Multiple And Boolean Statement Dim Age As Integer Dim Gender As String Age = Console.ReadLine Gender = Console.ReadLine If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? Console.WriteLine(“Allow into nightclub.”) Console.WriteLine(“Allow into nightclub.”) Else ‘ Everybody else Console.WriteLine(“Do not allow into nightclub.”) Console.WriteLine(“Do not allow into nightclub.”) End If

6 6 Nested If Structure We could use:

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

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

9 9 ElseIf structures So instead of:

10 10 ElseIf structures Dim Mark As Integer Mark = Console.ReadLine If Mark >=60 Then ‘Mark 60 or more? Console.WriteLine(“Merit”) Console.WriteLine(“Merit”) ElseIf Mark >= 40 Then ‘Mark 40 - 59? Console.WriteLine(“Pass”) Console.WriteLine(“Pass”) Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If

11 11 Nested If Structures We can use:

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

13 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 14 Program 2.3a 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 15 Program 2.3a Rent a property Dim Type As String Dim Bedrooms As Integer Console.WriteLine(“Please enter the property type.”) Type = Console.ReadLine Console.WriteLine(“Please enter the number of bedrooms.”) Bedrooms = Console.ReadLine If Type = "Cottage" Or Type = "Detached" And Bedrooms >= 4 Then Console.WriteLine("Rent it!") Console.WriteLine("Rent it!")Else Console.WriteLine("Don’t rent it!") Console.WriteLine("Don’t rent it!") End If

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

17 17 Program 2.3a 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!’.

18 18 Program 2.3a Rent a property This is because And is done before Or. So the program interprets the code like this: Type = "Cottage" Type = "Cottage" Or Or Type = "Detached" And Bedrooms >= 4 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.

19 19 Program 2.3a Rent a property Replace the previous code with: If (Type = "Cottage" And Bedrooms >= 4) Or (Type = "Detached" And Bedrooms >= 4) Then If (Type = "Cottage" And Bedrooms >= 4) Or (Type = "Detached" And Bedrooms >= 4) Then Console.WriteLine ("Rent it!") Else Else Console.WriteLine ("Don’t rent it!") End If End If

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

21 21 Extension “Deciding Exam Grades” Program 2.3b Change the “Deciding Exam Grades” Program written in 2.1 / 2.2 Selection. 2.12.22.12.2 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. 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). With a final “Else” to produce the general error message: With a final “Else” to produce the general error message: “You have entered an invalid mark!” Note: 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 Version 2.3b).

22 22 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 "26/10/20151 2.3 Selection Nested If structures & Complex Multiple Conditions."

Similar presentations


Ads by Google