Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Structures Tonga Institute of Higher Education.

Similar presentations


Presentation on theme: "Selection Structures Tonga Institute of Higher Education."— Presentation transcript:

1 Selection Structures Tonga Institute of Higher Education

2 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 Statement  Select Statement

3 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

4 Selection Structures/Statements Selection structures allow programs to run different code based on runtime conditions.  If Statement  Select Statement

5 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!”

6 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!” Keywords are blue Code to run if True Code to run if False Keywords are blue

7 IF Statements - 1 You can execute many lines of code

8 IF Statements - 2 You don’t need an Else statement

9 Nested If Statements Check for different conditions.

10 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: >=

11 Demonstration If Statements

12 Logical Operators Logical operators return a true or false value as a result of performing an operation on two booleans. 1. And 2. 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 > 1000 Or ChickenQuantity > 5000 Or PigQuantity > 5000

13 Logical Operators - And And  Both conditions must be true to return true. Otherwise, false is returned. Condition 1OperatorCondition 2Result TrueAndTrue AndFalse AndTrueFalse AndFalse

14 Logical Operators - Or Or  At least one condition must be true to return true. Otherwise, false is returned. Condition 1OperatorCondition 2Result TrueOrTrue OrFalseTrue FalseOrTrue FalseOrFalse

15 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”

16 Demonstration If Statements with Boolean Logic

17 Select Statement / Structure This allows a program to run different code based on a condition. Improves readability of very long nested if statements

18 Demonstration Select Statements


Download ppt "Selection Structures Tonga Institute of Higher Education."

Similar presentations


Ads by Google