Selection Structures Tonga Institute of Higher Education.

18
Selection Structures Tonga Institute of Higher Education

Transcript of Selection Structures Tonga Institute of Higher Education.

Page 1: Selection Structures Tonga Institute of Higher Education.

Selection Structures

Tonga Institute of Higher Education

Page 2: Selection Structures Tonga Institute of Higher Education.

Introduction

Programs must be able to adapt to conditions encountered when the program is running.

Selection structures allow programs to adapt to different conditions. IF…Else StatementSelect Statement

Page 3: Selection Structures Tonga Institute of Higher Education.

Source Code Execution Order

Generally, source code is run in order

But we don’t always need to run in order

This is run first

This is run second

This is run third

Page 4: Selection Structures Tonga Institute of Higher Education.

Selection Structures/Statements

Selection structures allow programs to run different code based on runtime conditions. If StatementSelect Statement

Page 5: Selection Structures Tonga Institute of Higher Education.

If Statement / Structure

This allows a program to run different code based on a condition.

They require 3 things:1. The condition to test for.2. The code to run if the condition is true.3. The code to run if the condition is false.

Example:1. Condition: If my name is Dave.2. If True: Print “Your name is Dave!”3. If False: Print “Your name is not Dave!”

Page 6: Selection Structures Tonga Institute of Higher Education.

If Statement Overview Example:

1. Condition: If my name is Dave.2. If True: Print “Your name is Dave!”3. If False: Print “Your name is not Dave!”

Keywordsare blue

Code to run if True

Code to run if False

Keywordsare blue

Page 7: Selection Structures Tonga Institute of Higher Education.

IF Statements - 1

You can executemany lines of code

Page 8: Selection Structures Tonga Institute of Higher Education.

IF Statements - 2

You don’t needan Else statement

Page 9: Selection Structures Tonga Institute of Higher Education.

Nested If Statements

Check fordifferent conditions.

Page 10: Selection Structures Tonga Institute of Higher Education.

Comparison Operators The return of a comparison operator is a boolean

(true/false)

Equal: =

Not Equal: <>

Less Than: <

Less Than or Equal To: <=

Greater Than: >

Greater Than or Equal To: >=

Page 11: Selection Structures Tonga Institute of Higher Education.

Demonstration

If Statements

Page 12: Selection Structures Tonga Institute of Higher Education.

Logical Operators Logical operators return a true or false value as a result

of performing an operation on two booleans.1. And2. Or

Use these to evaluate multiple conditions. Example: We own a restaurant. We want to buy

chicken and pigs from large suppliers. Our criteria for finding a supplier could be:

ChickenQuantity > 1000 And PigQuantity > 1000Or

ChickenQuantity > 5000 Or

PigQuantity > 5000

Page 13: Selection Structures Tonga Institute of Higher Education.

Logical Operators - And

And Both conditions must be true to return true.

Otherwise, false is returned.

Condition 1 Operator Condition 2 Result

True And True True

True And False False

False And True False

False And False False

Page 14: Selection Structures Tonga Institute of Higher Education.

Logical Operators - Or

Or At least one condition must be true to return true.

Otherwise, false is returned.

Condition 1 Operator Condition 2 Result

True Or True True

True Or False True

False Or True True

False Or False False

Page 15: Selection Structures Tonga Institute of Higher Education.

Practice!Dim Name1 as String = “Dave”

Dim Name2 as String = “Cathy”

Dim Name3 as String = “Jenny”

1. (Name1 = “Bob” And Name2 = “Cathy”) Or Name3 = “Jenny”

2. Name1 = “Dave” Or Name2 <> “Bob”

3. (Name1 <> “Dave” And Name2 = “Bob”) Or Name3 = “Jenny”

Page 16: Selection Structures Tonga Institute of Higher Education.

Demonstration

If Statements with Boolean Logic

Page 17: Selection Structures Tonga Institute of Higher Education.

Select Statement / Structure This allows a program to run different code based on a

condition. Improves readability of very long nested if statements

Page 18: Selection Structures Tonga Institute of Higher Education.

Demonstration

Select Statements