Selection Structures - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Selection Structures

Description:

Selection Structures Tonga Institute of Higher Education ... We want to buy chicken and pigs from large suppliers. Our criteria for finding a supplier could be: ... – PowerPoint PPT presentation

Number of Views:171
Avg rating:3.0/5.0
Slides: 19
Provided by: DaveS215
Learn more at: http://www.tihe.org
Category:

less

Transcript and Presenter's Notes

Title: Selection Structures


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.
  • IFElse Statement
  • Select Statement

3
Source Code Execution Order
  • Generally, source code is run in order

This is run first
This is run second
This is run third
  • But we dont always need to run in order

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
  • The condition to test for.
  • The code to run if the condition is true.
  • The code to run if the condition is false.
  • Example
  • Condition If my name is Dave.
  • If True Print Your name is Dave!
  • If False Print Your name is not Dave!

6
If Statement Overview
  • Example
  • Condition If my name is Dave.
  • If True Print Your name is Dave!
  • If False Print Your name is not Dave!

Keywords are blue
Code to run if True
Keywords are blue
Code to run if False
7
IF Statements - 1
You can execute many lines of code
8
IF Statements - 2
You dont 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 ltgt
  • Less Than lt
  • Less Than or Equal To lt
  • Greater Than gt
  • Greater Than or Equal To gt

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.
  • And
  • 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 gt 1000 And PigQuantity gt 1000
  • Or
  • ChickenQuantity gt 5000
  • Or
  • PigQuantity gt 5000

13
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
14
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
15
Practice!
  • Dim Name1 as String Dave
  • Dim Name2 as String Cathy
  • Dim Name3 as String Jenny
  • (Name1 Bob And Name2 Cathy) Or Name3
    Jenny
  • Name1 Dave Or Name2 ltgt Bob
  • (Name1 ltgt 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
Write a Comment
User Comments (0)
About PowerShow.com