Decisions - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Decisions

Description:

Convert temperature from fahrenheit to celsius or from. celsius to fahrenheit based on C or F ... Write the statements necessary to input the price of apples ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 28
Provided by: btay4
Category:

less

Transcript and Presenter's Notes

Title: Decisions


1
Decisions
  • Given hours worked and pay rate, calculate total
    pay
  • What if you work overtime?
  • How do you indicate if your work overtime?
  • How about double overtime?

2
Relational Operators
  • lt less than
  • lt less than or equal to
  • gt greater than
  • gt greater than or equal to
  • equals
  • ! not equal to

3
Examples
  1. 10 gt 2
  2. 'a' lt 'c' Why is this true?
  3. x ! y
  4. temp gt humidity
  5. num 35
  6. initial ! Q

4
Logicals AND OR
NOT !
5
Examples
  • 1. if ((sex 'F') (city_code 18) (gpa
    gt 3.7))
  • cout ltlt "Merit Award" ltlt endl
  • 2. if ((zip "48002") (zip "48003")
    (zip "48004"))
  • cout ltlt "LOCAL ltlt endl

6
Short-Circuit Evaluation
  • Process in which the computer evaluates a logical
    expression from left to right and stops as soon
    as the value of the expression is known
  • one True in -gt True
  • one False in -gt False
  • Example
  • int age
  • int height
  • age 25
  • height 70
  • ((age gt 50) (height gt 60))
  • 2. ((height gt 60) (age gt 40))

7
Selection/Decision
  • Test true or false
  • One operation performed if true
  • One operation performed if false
  • Keywords
  • if, else

8
if Syntax
  • if ( Expression )
  • statement
  • NOTE statement can be
  • a single statement,
  • a null statement, or
  • a compound statement.

9
if statement is a selection
  • of whether or not to execute a statement (which
    can be a single statement or an entire block)

TRUE
expression
FALSE
statement
10
Examples
  • if (taxCode T )
  • price price taxRatePrice
  • 2. if (num lt 0 )
  • // compound statement
  • cout ltlt "Negative" ltlt endl
  • numNegs numNegs 1
  • 3. if (age lt 21 )
  • cout ltlt "Under Age"

11
if-else Syntax
  • if ( Expression )
  • StatementA
  • else
  • StatementB
  • NOTE StatementA and StatementB each can be
  • a single statement
  • a null statement, or
  • a compound statement.

12
if..else provides two-way selection
  • between executing one of 2 clauses (the if clause
    or the else clause)

TRUE
FALSE
expression
if clause
else clause
13
  • int carDoors, driverAge
  • float premium, monthlyPayment
  • . . .
  • if ( (carDoors 4 ) (driverAge gt 24) )
  • premium 650.00
  • cout ltlt "LOW RISK ltlt endl
  • else
  • premium 1200.00
  • cout ltlt HIGH RISK ltlt endl
  • monthlyPayment premium / 12.0 5.00

14
Example
Convert temperature from fahrenheit to celsius or
from celsius to fahrenheit based on C or F
Input Process Output
temp Input temp, units newTemp
units Convert temperature
Output newTemp
15
code
  • int main()
  • float temp
  • float newTemp
  • char units
  • cout ltlt " Enter temperature and C or F"
  • cin gtgt temp gtgt units
  • if (units 'F')
  • newTemp (5 (temp 32) )/9
  • cout ltlt templtlt "Degrees F " ltltnewTempltlt"
    Degrees C
  • else
  • newTemp ((9 temp)/5) 32
  • cout ltlt "Degrees C " ltlt newTemp ltlt " Degrees
    F ltlt endl
  • // end main

16
Exercises
  1. Write the statements that allow you to input a
    number and prints whether it is positive or
    negative.
  2. Write the statements necessary to input the price
    of apples and the price of oranges. Then print
    either "THE APPLES COST MORE" or "THE ORANGES
    COST MORE. How would you handle the situation
    where the cost was the same?
  3. Write the statements that allow you to input a
    number and prints whether it is odd or even.

17
Nested selection - Example
  • if (hrsWorked lt 40 )
  • totPay hrsWorked payRate
  • else
  • regPay 40 payRate
  • if (hrsWorked lt 60)
  • otPay (hrsWorked 40) (1.5 payRate)
  • totPay regPay otPay
  • else
  • otPay 20 (1.5 payRate)
  • doubleOtPay (hrsWorked 60) (2 payRate)
  • grossPay regPay otPay doubleOtPay

18
Writing a nested if statement as a
Multiple-Alternative Decision
  • if (x gt 0)
  • cout ltlt Positive ltlt endl
  • else
  • if (x lt 0)
  • cout ltlt Negative ltlt endl
  • else
  • cout ltlt Zero ltlt endl
  • if (x gt 0)
  • cout ltlt Positive ltlt endl
  • else if (x lt 0)
  • cout ltlt Negative ltlt endl
  • else
  • cout ltlt Zero ltlt endl

19
  • cin gtgt testGrade
  • if (testGrade gt 90)
  • letterGrade A
  • else if (testGrade gt 80) testGrade lt 90
  • letterGrade 'B
  • else if (testGrade gt 70) testGrade lt 80
  • letterGrade 'C
  • else if (testGrade gt 60) testGrade lt 70
  • letterGrade 'D
  • else if testGrade lt 60
  • letterGrade 'F
  • Why is italicized code not necessary?

20
  • cin gtgt testGrade
  • if (testGrade gt 90)
  • letterGrade 'A
  • else if (testGrade gt 80 )
  • letterGrade 'B
  • else if (testGrade gt 70 )
  • letterGrade 'C
  • else if (testGrade gt 60 )
  • letterGrade 'D
  • else
  • letterGrade 'F

21
Nested if Statements
  • if ( Expression1 )
  • Statement1
  • else if ( Expression2 )
  • Statement2
  • .
  • .
  • .
  • else if ( ExpressionN )
  • StatementN
  • else
  • Statement N1
  • EXACTLY 1 of these statements will be executed.

22
Multi-way Branching
  • if ( creditsEarned gt 90 )
  • cout ltlt SENIOR STATUS
  • else if ( creditsEarned gt 60 )
  • cout ltlt JUNIOR STATUS
  • else if ( creditsEarned gt 30 )
  • cout ltlt SOPHOMORE STATUS
  • else
  • cout ltlt FRESHMAN STATUS

23
Use nested logic rather than straight thru logic
  • if (age lt 16)
  • charge 7
  • if ((age gt 16) (age lt 65))
  • charge 10
  • if (age gt 65)
  • charge 5
  • if (age lt 16 )
  • charge 7
  • else if ((age gt 16) (age lt 65))
  • charge 10
  • else if (age gt 65 )
  • charge 5

24
Testing Selection Control Structures
  • to test a program with branches, use enough data
    sets so that every branch is executed at least
    once
  • this is called minimum complete coverage

25
switch
  • Use for multiple tests of one variable
  • switch (selector)
  • case label1
  • statement 1
  • case label2
  • statement 2
  • ..
  • case label n
  • statement n
  • default
  • statement e

26
Example
  • cin gtgt menuOpt
  • switch (menuOpt)
  • case 1
  • cout ltlt "Beginners
  • break
  • case 2
  • cout ltlt "Advanced Beginner
  • break
  • case 3
  • cout ltlt "Intermediate
  • break
  • default
  • cout ltlt "Invalid Option
  • //END CASE

27
Example
  • cin gtgt menuOpt
  • switch (menuOpt)
  • case 1
  • case 2
  • cout ltlt Buckle my shoe
  • break
  • case 3
  • case 4
  • cout ltlt Shut the door
  • break
  • case 5
  • case 6
  • cout ltlt Pick up stickse
  • break
  • default
  • cout ltlt "Invalid Option
  • //END CASE
Write a Comment
User Comments (0)
About PowerShow.com