Decision Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Decision Structures

Description:

Works both for numbers and strings. Logical Operators. And, Or, Not. a =b And b 2. a =b Or b 2. And statements: Both must be true ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 22
Provided by: geraldu
Learn more at: https://www.msu.edu
Category:

less

Transcript and Presenter's Notes

Title: Decision Structures


1
Decision Structures
2
Spacecraft
  • Divine Vessel V will orbits earth 14 times in 21
    hours
  • First manned spacecraft of China, only the third
    country that can send

3
Decisions (If Statements)
  • Comparison Operators (gt, lt , ltgt, )
  • Logical Operators (and, or, not)
  • TRUE or FALSE determines whether to proceed!

4
Questions
If Temp lt 32 Then
True
False
It is warm!
It is COLD!
5
If Statements
  • If weather PRECIPITATION Then
  • It is raining or snowing
  • If TEMP lt 32 Then
  • It is COLD!
  • If temp lt 32 Then
  • picture1.print It is FREEZING!
  • End If

6
Comparison Operators
  • See table 5.1
  • equal to
  • ltgt unequal to
  • lt less than
  • gt greater than
  • lt less than or equal to
  • gt greater than or equal to
  • Works both for numbers and strings.

7
Logical Operators
  • And, Or, Not
  • a gtb And b ltgt 2
  • a gtb Or b ltgt 2
  • And statements Both must be true
  • Or statements one or both must be true
  • Not statements True if statement is false

8
Boolean value
  • Comparison operator and logical operator will
    return a Boolean value.
  • Boolean value has only two possibilities True
    and False

9
Test for Snowing
  • What are conditions for snowing?
  • Temperature?
  • Precipitation?

10
Ands and Ors
  • If (weather PRECIPITATION) And (TEMP lt 32)
    Then
  • Tests for snow
  • If (weather PRECIPITATION) Or (TEMP lt 32)
    Then
  • Tests for bad weather

And
Or
NO
NO
PRECIP
PRECIP
lt32
lt 32
T
F
T
T
F
F
gt32
gt 32
F
T
11
If Blocks (True and False Options)
  • If condition1 Then
  • Statements
  • Else
  • Statements
  • End If

12
If-then-else Blocks
  • If temp lt 32 Then
  • picture1.print It is FREEZING!
  • Else
  • picture1.print It is not freezing
  • End If

13
Nested if-then-else structure
  • If weather PRECIPITATION then
  • If TEMP lt32 then
  • Picture1.Print Snowing
  • Else
  • Picture1.Print raining
  • End if
  • Else
  • If TEMP lt32 then
  • Picture1.Print cold day
  • Else
  • Picture1.Print warm day
  • End if
  • End if

14
If ElseIf Else Blocks
  • Get answer to question, Did you
  • have a fun spring break?
  • answer Ucase(text1.text)
  • answer Trim(answer)
  • If answerYES Then
  • picture1.print Cool.
  • ElseIf answerNO Then
  • picture1.print Sorry.
  • Else
  • picture1.print Cant you even answer the
    question?
  • End If

15
What if you have a bunch of choices?
  • Case Selectors are great for multiple choices
  • Best for equalities, not inequalities

16
Select Case Blocks
  • Allows multiple options (not just true or false)
  • Select Case Variable
  • Case Condition1
  • Case Condition2
  • Case Condition3
  • Case Else
  • End Select

17
Name Case
  • Select Case Name
  • Case George W. Bush
  • picture1.print Bring them on.
  • Case terminator T-100
  • picture1.print Alta vista babe.
  • Case Conan O'Brien
  • picture1.print This is such a good show.
    What a nice show... Great show
  • Case Else
  • picture1.print Wow, who is this guy!
  • End Select

18
Quadratic equation
  • aX2bXc0
  • What is the solution to X?

19
Solution
20
Quadratic Formula Solver
21
Quadratic Equation
  • Write an algorithm for using the quadratic
    formula to determine if there are real roots for
    the equation
  • Will the program crash if b2-4ac lt 0?
  • If you take square root for a negative number you
    will get a run time error.
  • The program should be able to deal with both real
    and complex solutions.
Write a Comment
User Comments (0)
About PowerShow.com