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

22
03/16/22 03/16/22 1 2.3 Selection 2.3 Selection Nested If structures Nested If structures & & Complex Multiple Complex Multiple Conditions Conditions

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

04/20/2304/20/23 11

2.3 Selection2.3 Selection

Nested If structuresNested If structures

&&

Complex Multiple ConditionsComplex Multiple Conditions

22

Learning ObjectivesLearning Objectives

State what is needed for each State what is needed for each IfIf statement statement in in Nested If StructuresNested If Structures..

State which has precedence: State which has precedence: AndAnd / / Or Or Boolean statements Boolean statements and note that this and note that this can cause problems in complex multiple can cause problems in complex multiple conditions.conditions.

33

Nested If StructuresNested If Structures

An alternative to using multiple And An alternative to using multiple And conditions.conditions.

44

Multiple Multiple And And Boolean StatementBoolean Statement

So instead of using a multiple So instead of using a multiple AndAnd condition like:condition like:

55

MultipleMultiple And And Boolean StatementBoolean Statement

Dim Age As IntegerDim Age As IntegerDim Gender As StringDim Gender As StringAge = Console.ReadLine Age = Console.ReadLine Gender = Console.ReadLineGender = Console.ReadLineIf (Age >= 18) And (Gender = “F”) Then If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ Female ‘ 18 and over?‘ 18 and over? Console.WriteLine(“Allow into nightclub.”)Console.WriteLine(“Allow into nightclub.”)

Else Else ‘ Everybody else‘ Everybody else Console.WriteLine(“Do not allow into nightclub.”)Console.WriteLine(“Do not allow into nightclub.”)

End IfEnd If

66

Nested Nested IfIf Structure Structure

We could use:We could use:

77

Nested Nested IfIf Structure StructureDim Age As IntegerDim Age As Integer

Dim Gender As StringDim Gender As String

Age = Console.ReadLine Age = Console.ReadLine

Gender = Console.ReadLineGender = Console.ReadLine

If Age >= 18 Then If Age >= 18 Then ‘Aged 18 or over?‘Aged 18 or over? If Gender = “F” Then If Gender = “F” Then ‘and female?‘and female?

Console.WriteLine(“Allow into nightclub.”)Console.WriteLine(“Allow into nightclub.”) Else Else ‘All other people.‘All other people.

Console.WriteLine(“Do not allow into nightclub!”)Console.WriteLine(“Do not allow into nightclub!”) End IfEnd If

End IfEnd If

11 22

88

Nested Nested IfIf Structures Structures

We can also replace We can also replace ElseIfElseIf structures with structures with nested nested IfIf structures. structures.

99

ElseIfElseIf structures structures

So instead of:So instead of:

1010

ElseIfElseIf structures structures

Dim Mark As IntegerDim Mark As Integer

Mark = Console.ReadLine Mark = Console.ReadLine

If Mark >=60 Then If Mark >=60 Then ‘Mark 60 or more?‘Mark 60 or more? Console.WriteLine(“Merit”)Console.WriteLine(“Merit”)

ElseIf Mark >= 40 Then ElseIf Mark >= 40 Then ‘Mark 40 - 59?‘Mark 40 - 59? Console.WriteLine(“Pass”)Console.WriteLine(“Pass”)

Else Else ‘Mark under 40.‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”)Console.WriteLine(“A mark of “ & Mark & “ is a fail.”)

End IfEnd If

1111

Nested Nested IfIf Structures Structures

We can use:We can use:

1212

Nested Nested IfIf Structures Structures

If Mark >=60 Then If Mark >=60 Then ‘Mark 60 or more?‘Mark 60 or more? Console.WriteLine(“Merit”)Console.WriteLine(“Merit”)

ElseElse If Mark >= 40 Then If Mark >= 40 Then ‘Mark 40 - 59?‘Mark 40 - 59?

Console.WriteLine(“Pass”)Console.WriteLine(“Pass”) Else Else ‘Mark under 40.‘Mark under 40.

Console.WriteLine(“A mark of “ & Mark & “ is a fail.”)Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End IfEnd If

End IfEnd If

11 22

1313

Nested Nested IfIf Structures Structures

Note:Note: If you use nested If you use nested IfIf structures you need to structures you need to

remember to use an remember to use an EndEnd IfIf statement for statement for each each IfIf statement. statement.

1414

Program 2.3a Rent a propertyProgram 2.3a Rent a property

Specification:Specification: Illustrate complex multiple conditions:Illustrate complex multiple conditions:

More specifically to illustrate the order of More specifically to illustrate the order of precedence of the precedence of the AndAnd & & OrOr logical operators. logical operators.

A customer wants to rent a holiday property A customer wants to rent a holiday property which has 4 or more bedrooms, and it must which has 4 or more bedrooms, and it must be a cottage or a detached house.be a cottage or a detached house.

1515

Program 2.3a Rent a propertyProgram 2.3a Rent a property

Dim Type As StringDim Type As StringDim Bedrooms As IntegerDim Bedrooms As IntegerConsole.WriteLine(“Please enter the property type.”)Console.WriteLine(“Please enter the property type.”)Type = Console.ReadLineType = Console.ReadLineConsole.WriteLine(“Please enter the number of Console.WriteLine(“Please enter the number of bedrooms.”)bedrooms.”)Bedrooms = Console.ReadLineBedrooms = Console.ReadLineIf If Type Type = "Cottage" Or = "Cottage" Or Type Type = "Detached" And = "Detached" And Bedrooms >Bedrooms >== 4 Then 4 Then

Console.WriteLineConsole.WriteLine("Rent it!")("Rent it!")

ElseElse Console.WriteLineConsole.WriteLine("Don’t rent it!")("Don’t rent it!")

End IfEnd If

1616

Program 2.3a Rent a propertyProgram 2.3a Rent a property

Run the program and test all possibilities.Run the program and test all possibilities.

You should find a problem! What is it?You should find a problem! What is it?

1717

Program 2.3a Rent a propertyProgram 2.3a Rent a property

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

1818

Program 2.3a Rent a propertyProgram 2.3a Rent a property

This is because This is because AndAnd is done before is done before OrOr..So the program interprets the code like this:So the program interprets the code like this: Type = "Cottage"Type = "Cottage" OrOr Type = "Detached" Type = "Detached" AndAnd Bedrooms > Bedrooms >== 4 4

So if it is Detached then this works as both the So if it is Detached then this works as both the type and the number of bedrooms has to be true type and the number of bedrooms has to be true but if it is a cottage then the type but if it is a cottage then the type oror the number the number of bedrooms is required to be true; so it doesn’t of bedrooms is required to be true; so it doesn’t work.work.

1919

Program 2.3a Rent a propertyProgram 2.3a Rent a property

Replace the previous code with:Replace the previous code with: If (TypeIf (Type = "Cottage" And Bedrooms >= "Cottage" And Bedrooms >== 4) 4) Or Or

(Type = "Detached" And Bedrooms(Type = "Detached" And Bedrooms >>== 4) Then 4) Then

Console.WriteLineConsole.WriteLine("Rent it!")("Rent it!") ElseElse

Console.WriteLineConsole.WriteLine("Don’t rent it!")("Don’t rent it!") End IfEnd If

2020

Program 2.3a Rent a propertyProgram 2.3a Rent a property

Run the program and test all possibilities.Run the program and test all possibilities.

It should work perfectly now.It should work perfectly now.

2121

Extension “Deciding Exam Grades” Extension “Deciding Exam Grades” Program 2.3bProgram 2.3b

Change the “Deciding Exam Grades” Program Change the “Deciding Exam Grades” Program written in written in 2.1 / 2.2 Selection.2.1 / 2.2 Selection. Use a logical expression that is Use a logical expression that is true when the mark true when the mark

is within the rangeis within the range and false when the mark is and false when the mark is outside the range of 0 – 100 and.outside the range of 0 – 100 and.

Therefore use one IF statement with an Therefore use one IF statement with an initialinitial IF test with IF test with AndAnd, to test if the mark is valid proceeded with a , to test if the mark is valid proceeded with a nestednested IF IF statementstatement to produce the appropriate valid messages: to produce the appropriate valid messages:Merit (60 or more), Pass (40 – 59), Fail (under 40).Merit (60 or more), Pass (40 – 59), Fail (under 40).

With a final “Else” to produce the general error With a final “Else” to produce the general error message:message:

““You have entered an invalid mark!”You have entered an invalid mark!”

Note:Note:Make a copy of the previous program’s whole folder to Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the keep the original program but rename this folder with the same name but add (same name but add (Nested If Version 2.3bNested If Version 2.3b).).

2222

PlenaryPlenary

What is needed for each What is needed for each IfIf statement in statement in Nested If StructuresNested If Structures?? End IfEnd If

Which has precedence: Which has precedence: AndAnd / / OrOr?? AndAnd