Selection Structures Tonga Institute of Higher Education.
-
Upload
tracey-lloyd -
Category
Documents
-
view
213 -
download
0
Transcript of Selection Structures Tonga Institute of Higher Education.
![Page 1: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/1.jpg)
Selection Structures
Tonga Institute of Higher Education
![Page 2: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/7.jpg)
IF Statements - 1
You can executemany lines of code
![Page 8: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/8.jpg)
IF Statements - 2
You don’t needan Else statement
![Page 9: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/9.jpg)
Nested If Statements
Check fordifferent conditions.
![Page 10: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/11.jpg)
Demonstration
If Statements
![Page 12: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/16.jpg)
Demonstration
If Statements with Boolean Logic
![Page 17: Selection Structures Tonga Institute of Higher Education.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022071806/56649d0e5503460f949e455d/html5/thumbnails/18.jpg)
Demonstration
Select Statements