Making Decisions - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Making Decisions

Description:

Character values to Character values ... 1. Each language supports its own set of comparison operators that express Boolean tests. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 34
Provided by: jason209
Category:

less

Transcript and Presenter's Notes

Title: Making Decisions


1
Chapter 05
  • Making Decisions

2
Evaluating Boolean Expressions to Make
Comparisons
  • Types of Selection Structure
  • Single-Alternative Selection (Unary or
    Single-Sided)
  • Action is required for only one outcome of the
    question
  • If-then Selection Structure because no else
    action is necessary

3
(No Transcript)
4
  • Dual-Alternative Selection (Binary or
    Doubled-Sided)
  • Two possible outcomes depending on the answer to
    the question
  • Logical flow proceeds either to the left branch
    of the structure or to the right
  • The choices are mutually exclusive that is, the
    logic can flow only to one of the two
    alternatives, never both
  • If-then-else Selection Structure

5
(No Transcript)
6
  • Defining a Boolean Expression
  • 1. One that represents only one of two states -
    True or False
  • 2. Every decision you make in a computer program
    involves
  • evaluating a Boolean Expression
  • 3. True/False evaluation is natural from a
    computers standpoint
  • Binary Representation 0 (False, Off) or 1
    (True, On)

7
Using the Comparison Operators
  • Comparisons of Two Values of the Same Type
  • 1. The two values are equal
  • 2. The first value is greater than the second
    value
  • 3. The first value is less than the second value

8
  • Optional Comparisons of Two Values of the Same
    Type
  • 4. The first value is greater than or equal to
    the second
  • value
  • 5. The fist value is less than or equal to the
    second value
  • 6. The two values are not equal

9
  • What Can Be Compared
  • 1. Values that are of the same type
  • Numeric values to Numeric values
  • Character values to Character values
  • 2. The two values used can be either Variables or
    Constants
  • currentTotal 100 (variable to a constant)
  • currentTotal previousTotal (variable to a
    variable)
  • 3. Although legal, there is no logical reason to
    compare two constants, such expressions are
    trivial because each always results in the same
    value.
  • 20 20
  • 30 40

10
  • Comparison Operators and Common Symbols
  • (Figure 5-7, Logical Comparisons, page 156 )
  • 1. Each language supports its own set of
    comparison operators that express Boolean tests.
  • 2. Whenever you use a comparison operator, you
    must provide a value on each side of the operator
  • 3. Any logical situation can be expressed using
    just three types of
  • comparisons (1) equal, (2) greater than, (3)
    less than

11
  • 4. You never need the three additional
    comparisons (1) greater than
  • or equal, (2) less than or equal (3) not equal,
    but using them often
  • makes decisions more convenient
  • 5. Often when determining which comparison
    operator to use the one
  • that seems natural is usually the best

12
  • Using Negative (NOT) Logic
  • 1. Comparing two amounts to decide if the are not
    equal to each other
  • is the most confusing of all the comparisons
  • 2. Using not equal to in decisions involves
    thinking in double
  • negatives and makes you prone to include logical
    errors in your
  • programs
  • 3. The not equal to comparison operator is one
    most likely to be
  • different in the various programming languages
    you may use
  • 4. Is commonly used when working with files and
    checking for NOT
  • end of file (eof)

13
(No Transcript)
14
Understanding AND Logic
  • Introduction to Nested Selection or Nested if
  • 1. Often you need more than on selection
    structure to determine whether an action should
    take place this type of situation is known as an
    AND situation because two tests must be passed
  • 2. An AND situation requires a Nested Decision or
    Nested if, that is, a decision inside of
    another decision

15
(No Transcript)
16
Writing AND Decisions for Efficiency
  • 1. Decide which of the two decisions to make
    first
  • 2. Logically, either selection in an AND
    situation can come first
  • 3. You can often improve your programs
    performance by making an
  • appropriate choice as to which selection to make
    first

17
  • 4. Does it make a difference which question is
    asked first? Output?
  • Are the two decisions mutually exclusive?
  • Are the two decision dependent on each other?
  • Efficiency? Small number of records? Large
    Number?
  • General Rule In an AND situation, first ask
    the question that is less likely to be true.
  • this eliminates as many records as possible
    from having to go
  • through the second decision and speeds up
    processing time

18
Combining Decisions in an AND Situation
  • Logical AND Operator - allows you to ask two or
    more
  • questions in a single comparison.
  • the question that is placed first in the compound
    condition will be the first question evaluated
  • the computer can ask only one question at a time.
  • the Boolean expressions on both sides of the AND
    must be True for the complete expression to be
    True

19
(No Transcript)
20
Avoiding Common Errors in an AND Situation
  • 1. Make sure that the second decision is made
    entirely within the first decision.
  • 2. Use a complete Boolean Expression on Both
    sides of the AND
  • Correct if empRate gt 10.00 and empRate lt 12.00
    then
  • Incorrect if empRate gt 10.00 and lt 12.00 then

21
(No Transcript)
22
Understanding OR Logic
  • Either one condition must be met or some other
    condition
  • must be met in order for an event to take place.

23
(No Transcript)
24
Writing OR Decisions for Efficiency
  • 1. Placing one question before the other might
    make one algorithm
  • superior to the other if you have some
    background information
  • about the relative likelihood of each condition
    you are testing
  • General Rule In an OR situation, first ask the
    question that
  • is more likely to be true.
  • because a record qualifies for printing or
    displaying as soon as
  • it passes one test, asking the more likely
    question first
  • eliminates as many records as possible from
    having to go
  • through the second decision the time it takes
    to execute the
  • program is decreased

25
Combining Decisions in an OR Situation
  • Logical OR Operator - allow you to ask two or
    more questions in a
  • single comparison
  • only one of the listed conditions must be met for
    the resulting action to take place
  • the question that is placed first in the compound
    condition will be the first question evaluated
  • the computer can ask only one question at a time.
  • the Boolean expressions on one side or the other
    side of OR makes the complete expression True

26
(No Transcript)
27
Avoiding Common Errors in an OR Situation
  • 1. Developing an unstructured program
  • 2. Using OR too casually as a programmer it is
    up to you to clarify what really is being
    requested
  • often a request for A and B means a request for A
    or B

28
  • 3. Use a complete Boolean Expression on Both
    sides of the OR
  • Correct if response Y or response y then
  • Incorrect if response Y or y then

29
(No Transcript)
30
Using Selections within Ranges
  • 1. Business Programs often need to make
    selections based on a variable falling within a
    range of values
  • 2. To perform a range check, make comparisons
    using either the lowest or highest value in each
    range of values you are using to make selections.
  • See Figure 5-32, Using Low-end Values for a Range
    Check

31
Common Errors Using Range Checks
  • Two Common Errors When Using Range Checks
  • (both entail doing more work than is necessary)
  • 1. Asking one question to many, Figure 5-33
  • 2. Inefficient range selection, Figure 5-34

32
Using Decision Tables
  • Four Parts of a Decision Table
  • 1. Conditions
  • 2. Possible Combinations of Boolean Values for
    the Condition
  • 3. Possible Actions Based on the Conditions
  • 4. The actions that correspond to Each Boolean
    Value of Each Condition
  • See Figure 5-44, page 186

33
  • Usefulness of a Decision Table
  • 1. When the decision-making process becomes more
    complicated
  • 2. Serve as a useful graphic tool when you want
    to explain the decision-making process of a
    program to a user who is not familiar with
    flowcharting symbols
Write a Comment
User Comments (0)
About PowerShow.com