Making Decisions Selection Statements - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Making Decisions Selection Statements

Description:

boolean-expression. something that will evaluate to true or false, but note: ... Conditional logic allows you to take different paths through a program based on ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 23
Provided by: donaldl1
Category:

less

Transcript and Presenter's Notes

Title: Making Decisions Selection Statements


1
Making Decisions -- Selection Statements
  • "if" Statements

2
When Use a Selection Stmt?
  • Where you want to do different processing based
    on conditions (i.e., values) in the data using
    predefined criteria
  • Selection is also called
  • conditional logic
  • decision logic
  • if

3
Selection / Decision Stmts in C
  • if statement -- if
  • switch statement -- switch
  • conditional statement -- ?
  • other statements that embed selection/decision
  • for statement
  • while statement
  • do while

4
"if" Statement
  • Basic Structure of an "if" statement
  • if (boolean-expression)
  • statement-1
  • else
  • statement-2

5
Formatting If Statements
  • Formatting is crucial, since human readers will
    use the layout to interpret the logic
  • People rely upon the visual layout to interpret
    what the program will do
  • People do not read a program the way the computer
    does -- character by character
  • If you lay out your if statements incorrectly,
    you and other readers will have trouble
    discovering logic errors
  • But, the computer ignores the format!

6
Elements of an "if" Stmt (refer to earlier Basic
Structure slide)
  • boolean-expression
  • something that will evaluate to true or false,
    but note
  • any non non-zero value is considered true
  • zero is considered false
  • statement-1 (and statement-2)
  • any valid C statement, which may
  • be a simple C statement, or a
  • compound one

7
Simple and Compound Stmts
  • Simple statement -
  • one "line" ending in a semi-colon
  • Compound statement (also called a "block")
  • one or more "lines"
  • each ending with a semi-colon
  • whole "group" surrounded by braces
  • no semi-colon after the closing brace

8
Examples of Simple and Compound Statements
  • Examples of simple C statements
  • totDue (oldAmt - payment) 1.05 fee
  • testWindow.displayMessage("Enter name ")
  • Examples of compound C statements
  • TestWindow.displayMessage("Enter name ")
  • testWindow.displayMessage("Enter name ")
  • result 1.1

9
Relational Operators Used in Boolean Expressions
  • gt greater than
  • gt greater than or equal to
  • lt less than
  • lt less than or equal to
  • equal to (note two equal signs)
  • ! not equal

10
Boolean Expressions
  • What do each of the following evaluate to (10 gt
    12)
  • (3. lt 14.)
  • (27 27)
  • (2727)
  • (amt 27)
  • (27)
  • (sum 0)
  • ("Joe" lt "Sam")

11
Boolean Expressions (more)
  • What do each of the following evaluate to
  • (strcmp("Joe", "Sam"))
  • (48.6 5. / 2. gt 32.)
  • ('a' gt 'b')
  • (amtDue gt amtPaid)
  • (custName gt custBalance)
  • (myCar.getWeight(2400))

12
Evaluating Boolean Expressions Using Objects
  • What do each of the following evaluate to
  • (sam lt dan)
  • ("sam" lt "dan")
  • //note these are not T/F but -1, 0, and 1
  • (strcmp("Joe", "Sam")
  • (strcmp("Sam", "Sam")
  • (strcmp("Sam", "Joe")

13
Unary Operator (!)
  • The unary operator ! inverts a boolean value
  • Examples
  • !true is false
  • !false is true
  • !(5 5)
  • amt 387.
  • min 300.
  • !(amt lt min)

14
Truth Tables
  • We can combine boolean values using
  • logical and
  • logical or
  • This will allow us to evaluate such expressions
    as
  • (10 lt 14) (5 gt -5)
  • (total surplus) (balance gt 500000)

15
Truth Table for (logical and)
  • true true evaluates to true
  • true false evaluates to false
  • false true evaluates to false
  • false false evaluates to false

16
Truth Table for (logical or)
  • true true evaluates to true
  • true false evaluates to true
  • false true evaluates to true
  • false false evaluates to false

17
Some Samples
  • ('a' gt 'b') (5 lt 7)
  • ('a' gt 'b') (5 lt 7)
  • (atBats gt 100) (battingAverage gt 300)
  • (amtOwed gt amtPaid) ((OurDate.subtract(dueDa
    te, paidDate) gt 30)
  • Hint Use lots of parentheses

18
Variables with Boolean Values
  • Example 1
  • bool myBool
  • myBool (15.5 - 3. lt 12.)
  • cout ltlt "Result is " ltlt myBool ltlt endl
  • Example 2
  • bool condition1, condition2
  • char guestName20 "Ed"
  • condition1 true
  • condition2 strcmp(guestName, "Ed")
    //caution!!!!!
  • bool correctPerson condition1 condition2
  • What value does correctPerson contain?

19
Nested Ifs
  • Recall the basic structure of an if statement
  • if (boolean-expression)
  • statement-1
  • else
  • statement-2
  • Statement-1 or statement-2 can contain any type
    of statements, including if statements. If
    either or both contain an if statement, then you
    refer to this as nested ifs.

20
Structure and Format of Nested Ifs
  • if (boolean-expression-1)
  • if (boolean-expression-2)
  • statement-1
  • else
  • statement-2
  • else
  • if (boolean-expression-3)
  • statement-3
  • else
  • statement-4

21
Social Security Example of Nested Ifs
  • double yTDSocSec 3750, wage 1000.,
    socSecPayment
  • if (yTDSocSec lt 3757.20)
  • if (yTDSocSec (wage 0.062) lt 3757.20)
  • socSecPayment wage
    0.062
  • else
  • socSecPayment 3757.20 -
    yTDSocSec
  • else
  • socSecPayment 0.

22
Summary -- Conditional Logic
  • Conditional logic is necessary for most programs
  • Conditional logic allows you to take different
    paths through a program based on predefined
    criteria
  • The basic structure of an "if" statement is
  • if (boolean-expression)
  • statement-1
  • else
  • statement-2
Write a Comment
User Comments (0)
About PowerShow.com