5 chapter 51 making decisions programming logic and design, second edition, comprehensive 5

57
Chapter 5 1 5 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Upload: lionel-bridges

Post on 13-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 1

5

Making Decisions

Programming Logic and Design, Second Edition, Comprehensive

5

Page 2: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 2

5

Objectives

• After studying Chapter 5, you should be able to:

• Evaluate Boolean expressions to make comparisons

• Use the logical comparison operators

• Understand AND logic

• Write AND decisions for efficiency

• Combine decisions in an AND situation

• Avoid common errors in an AND situation

Page 3: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 3

5

Objectives

• After studying Chapter 5, you should be able to:

• Understand OR logic

• Avoid common errors in an OR situation

• Write OR decisions for efficiency

• Combine decisions in an OR situation

• Use selections within ranges

• Understand common errors using range checks

• Use decision tables

Page 4: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 4

5Evaluating Boolean

Expressions to Make Comparisons

• The selection structure (sometimes called a decision structure) involved in programs is one of the basic structures of structured programming

• You can refer to the structure in Figure 5-1 as a dual-alternative or binary selection because there are two possible outcomes: depending on the answer to the question represented by the diamond, the logical flow proceeds either to the left branch of the structure or to the right

Page 5: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 5

5Evaluating Boolean

Expressions to Make Comparisons

Page 6: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 6

5Evaluating Boolean

Expressions to Make Comparisons

• This selection structure also called an if-then-else structure because it fits the statement

• The flowchart segment in Figure 5-2 represents a single-alternative or unary selection where action is required for only one outcome of the question

• You call this form of the if-then-else structure an if-then, because no “else” action is necessary

Page 7: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 7

5Flowchart and Pseudocode

for Overtime Decision

Page 8: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 8

5Flowchart and Pseudocode

for Dental Plan Decision

Page 9: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 9

5Evaluating Boolean

Expressions to Make Comparisons

• The typical if-then decision in Figure 5-4 shows the employee’s paycheck being reduced if the employee participates in the dental plan

• No action is taken if the employee is not in the dental plan

• A Boolean expression is one that represents only one of two states, usually expressed as true or false

Page 10: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 10

5

Using Logical Comparison Operators

• Usually, you can compare only values that are of the same type; that is, you can compare numeric values to other numeric values and character values to other characters

• For any two values that are the same type, you can decide whether:

– The two values are equal

– The first value is greater than the second value

– The first value is less than the second value

Page 11: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 11

5

Using Logical Comparison Operators

• In any Boolean expression, the two values used can be either variables or constants

• Such expressions are considered trivial because each always results in the same value: true for the first expression and false for the second

• Each programming language supports its own set of logical comparison operators, or comparison symbols, that express these Boolean tests

Page 12: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 12

5

Using Logical Comparison Operators

• In addition to the three basic comparisons you can make, most programming languages provide three others

• For any two values that are the same type, you can decide whether:

– The first is greater than or equal to the second

– The first is less than or equal to the second

– The two are not equal

Page 13: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 13

5

Using Logical Comparison Operators

• Most programming languages allow you to express “greater than or equal to” by typing a greater-than sign immediately followed by an equal sign (>=)

• Any logical situation can be expressed using just three types of comparisons: equal, greater than, and less than

• In Figure 5-5, if the value of customerCode is equal to 1, the logical flow follows the false branch of the selection

Page 14: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 14

5

Using a Negative Comparison

Page 15: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 15

5Using the Positive Equivalent

of the Negative Comparison in Figure 5-5

Page 16: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 16

5

Using Logical Comparison Operators

• Besides being awkward to use, the not equal to comparison is the one most likely to be different in the various programming languages you may use

• Although NOT comparisons can be awkward to use, there are times when your meaning is clearest if you use one

Page 17: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 17

5

Understanding AND Logic

• Often you need more than one selection structure to determine whether an action should take place

• For example, suppose that your employer wants a report that lists workers who have registered for both insurance plans offered by the company: the medical plan and the dental plan

• This type of situation is known as an AND situation because the employee’s record must pass two tests—participation in the medical plan and participation in the dental plan—before you write that employee’s information on the report

Page 18: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 18

5

Understanding AND Logic

• An AND situation requires a nested decision or a nested if; that is, a decision “inside of” another decision

Page 19: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 19

5

Understanding AND Logic

Page 20: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 20

5Flowchart of Mainline Logic for Medical and

Dental Participant Report

Page 21: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 21

5Flowchart of Housekeeping()

Routine for Medical and Dental Participant

Page 22: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 22

5mainLoop() of Program that Lists Medical and

Dental Participants

Page 23: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 23

5Pseudocode of Medical and Dental Participants

Report Program

Page 24: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 24

5

Writing AND Decisions for Efficiency

• When you nest decisions because the resulting action requires that two conditions be true, you must decide which of the two decisions to make first

• Logically, either selection in an AND situation can come first

• However, when there are two selections, you often can improve your program’s performance by making an appropriate choice as to which selection to make first

Page 25: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 25

5

Writing AND Decisions for Efficiency

Page 26: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 26

5Combining Decisions

in an AND Situation

• Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator

• If you want to select employees who carry both medical and dental insurance, you can use nested ifs, or you can include both decisions in a single statement by writing empDentalIns = “Y” AND empMedicalIns = “Y”?

Page 27: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 27

5Combining Decisions

in an AND Situation

Page 28: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 28

5Avoiding Common

Errors in an AND Situation

• When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision

Page 29: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 29

5

Understanding OR Logic

• Sometimes you want to take action when

one or the other of two conditions is true

• This is called an OR situation because

either one condition must be met or some

other condition must be met in order for

an event to take place

Page 30: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 30

5Flowchart and Pseudocode for

mainLoop() that Prints Employees Who Have Medical or Dental Insurance

Page 31: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 31

5Avoiding Common

Errors in an OR Situation

• Logically, you can argue that the flowchart in Figure 5-21 is correct because the correct employees print

• However, this flowchart is not allowed because it is not structured

Page 32: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 32

5

Writing OR Decisions for Efficiency

• You can write a program that creates a report containing all employees who take either the medical or dental insurance by using the mainLoop() in either Figure 5-22 or Figure 5-23

• When you use the logic shown in Figure 5-22 to select employees who participate in either insurance plan, you first ask about medical insurance

• If you use Figure 5-23, you ask empDentalIns = “Y”?

Page 33: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 33

5

Writing OR Decisions for Efficiency

• Using either scenario, 950 employee records appear on the list, but the logic used in Figure 5-22 requires 1,100 decisions while the logic used in Figure 5-23 require 1,500 decisions

Page 34: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 34

5Alternate mainLoop()

to Select Employees with Medical or Dental Insurance

Page 35: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 35

5Combining Decisions

in an OR Situation

• Most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator

• When you use the logical OR operator, only one of the listed conditions must be met for the resulting action to take place

Page 36: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 36

5Computer Logic of

Program Containing an OR Decision Using an OR Operator

Page 37: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 37

5

Using Selections within Ranges

• Business programs often need to make selections based on a variable falling within a range of values

• To perform a range check, make comparisons using either the lowest or highest values in each range of values you are using to make selections

• The pseudocode representing the logic for choosing a supervisor name by using the high-end values appears in Figure 5-27

Page 38: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 38

5

Using Selections within Ranges

Page 39: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 39

5Using Low-End Values

for a Range Check

Page 40: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 40

5

Common Errors Using Range Checks

• Two common errors that occur when programmers perform range checks both entail doing more work than is necessary

• Figure 5-29 shows a range check in which the programmer has asked one question too many

• Similarly, Figure 5-30 shows the beginning of an inefficient range selection

Page 41: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 41

5

Inefficient Range Selection

Page 42: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 42

5Partial Example of

Inefficient Range Selection

Page 43: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 43

5

Using Decision Tables

• A decision table is a problem-analysis tool that consists of four parts:

– Conditions

– Possible combinations of Boolean values for the conditions

– Possible actions based on the conditions

– The actions that correspond to each Boolean value of each condition

Page 44: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 44

5Flowchart of Mainline

Logic for Residence Hall Report

Page 45: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 45

5Flowchart of getReady()

Module for Residence Hall Report

Page 46: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 46

5

Using Decision Tables

Page 47: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 47

5

Using Decision Tables

Page 48: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 48

5Decision Table for

Residence Hall Selection, Part 3 of 3

Page 49: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 49

5Flowchart for Residence Hall Selection, Part 1 of 5

Page 50: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 50

5Flowchart for Residence Hall Selection, Part 2 of 5

Page 51: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 51

5Flowchart for Residence Hall Selection, Part 3 of 5

Page 52: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 52

5Flowchart for Residence Hall Selection, Part 4 of 5

Page 53: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 53

5Flowchart for Residence Hall Selection, Part 5 of 5

Page 54: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 54

5

Summary

• Every decision you make in a computer program involves evaluating a Boolean expression

• For any two values that are the same type, you can use logical comparison operators to decide whether the two values are equal, the first value is greater than the second value, or the first value is less than the second value

• An AND situation occurs when two conditions must be true in order for a resulting action to take place

Page 55: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 55

5

Summary

• An AND situation requires a nested decision or a nested if

• In an AND situation, first ask the question that is less likely to be true

• Most programming languages allow you to ask two or more questions in a single comparison by using a logical AND operator

• When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely within the first decision, and that you use a complete Boolean expression on both sides of the AND

Page 56: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 56

5

Summary

• An OR situation occurs when you want to take action when one or the other of the two conditions is true

• Errors occur in OR situations when programmers do not maintain structure

• In an OR situation, first ask the question that is more likely to be true

• Most programming languages allow you to ask two or more questions in a single comparison by using a logical OR operator

Page 57: 5 Chapter 51 Making Decisions Programming Logic and Design, Second Edition, Comprehensive 5

Chapter 5 57

5

Summary

• To perform a range check, make comparisons with either the lowest or highest value in each range of values you are using

• Common errors that occur when programmers perform range checks include asking unnecessary and previously answered questions

• A decision table is a problem-analysis tool that consists of conditions, possible combinations of Boolean values for the conditions, possible actions based on the conditions, and the actions that correspond to each Boolean value of each condition