SlideShare a Scribd company logo
Programming Logic and Design
Eighth Edition
Chapter 4
Making Decisions
Objectives
In this chapter, you will learn about:
• Boolean expressions and the selection structure
• The relational comparison operators
• AND logic
• OR logic
• NOT logic
• Making selections within ranges
• Precedence when combining AND and OR operators
2Programming Logic and Design, Eighth Edition
Boolean Expressions
and the Selection Structure
• Boolean expressions can be only true or false
• Every computer decision yields a true-or-false,
yes-or-no, 1-or-0 result
• Used in every selection structure
3Programming Logic and Design, Eighth Edition
Boolean Expressions and the
Selection Structure (continued)
• Dual-alternative (or binary) selection structure
– Provides an action for each of two possible outcomes
4Programming Logic and Design, Eighth Edition
Figure 4-1 The dual-alternative selection structure
Boolean Expressions and the
Selection Structure (continued)
• Single-alternative (or unary) selection structure
– Action is provided for only one outcome
– if-then
5Programming Logic and Design, Eighth Edition
Figure 4-2 The single-alternative selection structure
6Programming Logic and Design, Eighth Edition
Figure 4-3 Flowchart and pseudocode for overtime payroll program
Boolean Expressions and the
Selection Structure (continued)
• if-then-else decision
–if-then clause
• Holds the action or actions that execute
when the tested condition in the
decision is true
–else clause
• Executes only when the tested condition
in the decision is false
7Programming Logic and Design, Eighth Edition
Using Relational
Comparison Operators
• Relational comparison operators
– Six types supported by all modern programming
languages
– Two values compared can be either variables or
constants
• Trivial expressions
– Will always evaluate to the same result
– Examples:
• 20 = 20? TRUE
• 30 = 40? FALSE
8Programming Logic and Design, Eighth Edition
9Programming Logic and Design, Eighth Edition Table 4-2 Relational comparison operators
Using Relational Comparison
Operators (continued)
• Any decision can be made with only three types
of comparisons: =, >, and <
• “Not equal” operator <>
–Involves thinking in double negatives
–Best to restrict usage to “if without an else”—
that is, only take action when some
comparison is false
10Programming Logic and Design, Eighth Edition
Using Relational Comparison
Operators (continued)
11Programming Logic and Design, Eighth Edition
Figure 4-5 Using a negative comparison
Using Relational Comparison
Operators (continued)
12Programming Logic and Design, Eighth Edition
Figure 4-6 Using the positive equivalent of the negative comparison in Figure 4-5
Avoiding a Common Error with
Relational Operators
• Common errors
–Using the wrong operator
•BIG > small
•small < BIG
–Missing the boundary or limit
required for a selection
13Programming Logic and Design, Eighth Edition
Understanding AND Logic
• Compound condition
–Asks multiple questions before an outcome
is determined
• AND decision
–Requires that both of two tests evaluate to
true
–Requires a nested decision (nested if) or
a cascading if statement
14Programming Logic and Design, Eighth Edition
15Programming Logic and Design, Eighth Edition
Figure 4-7 Flowchart and pseudocode
for cell phone billing program
Nesting AND Decisions
for Efficiency
• When nesting decisions
– Either selection can come first
• Performance time can be improved by asking
questions in the proper order
• In an AND decision, first ask the question that is less
likely to be true
– Eliminates as many instances of the second
decision as possible
– Speeds up processing time
16Programming Logic and Design, Eighth Edition
Using the AND Operator
• Conditional AND operator
– Ask two or more questions in a single comparison
– Each Boolean expression must be true for entire
expression to evaluate to true
• Truth tables
– Describe the truth of an entire expression based
on the truth of its parts
• Short-circuit evaluation
– Expression evaluated only as far as necessary to
determine truth
17Programming Logic and Design, Eighth Edition
Using the AND Operator (continued)
18Programming Logic and Design, Eighth Edition
Table 4-3 Truth table for the AND operator
19Programming Logic and Design, Eighth Edition Figure 4-9 Using an AND operator and the logic behind it
Using the
AND
Operator
(continued)
Avoiding Common Errors
in an AND Selection
• Second decision must be made entirely within the
first decision
• In most programming languages, logical AND is a
binary operator
– Requires a complete Boolean expression on both
sides
20Programming Logic and Design, Eighth Edition
Understanding OR Logic
• OR decision
–Take action when one or the other of two
conditions is true
–Example
• “Are you free for dinner Friday or
Saturday?”
21Programming Logic and Design, Eighth Edition
Writing OR Selections for Efficiency
• May ask either question first
–Both produce the same output but vary
widely in number of questions asked
• If first question is true, no need to ask second
• In an OR decision, first ask the question that
is more likely to be true
–Eliminate as many extra decisions as
possible
22Programming Logic and Design, Eighth Edition
Using the OR Operator
• Conditional OR operator
– Ask two or more questions in a single
comparison
• Only one Boolean expression in an OR selection
must be true to produce a result of true
• Question placed first will be asked first
– Consider efficiency
• Computer can ask only one question at a time
23Programming Logic and Design, Eighth Edition
Using the OR Operator
(continued)
24Programming Logic and Design, Eighth Edition
Table 4-4 Truth table for the OR operator
25Programming Logic and Design, Eighth Edition
Figure 4-13 Using an OR operator and the logic behind it
Using the OR Operator
(continued)
Avoiding Common Errors
in an OR Selection
• Second question must be a self-contained structure with
one entry and exit point
• Request for A and B in English logically means a request for
A or B
– Examples
• “Add $20 to the bill of anyone who makes more than
100 calls and to anyone who has used more than 500
minutes”
• “Add $20 to the bill of anyone who has made more
than 100 calls or has used more than 500 minutes”
26Programming Logic and Design, Eighth Edition
Avoiding Common Errors
in an OR Selection (continued)
27Programming Logic and Design, Eighth Edition
Figure 4-14 Unstructured flowchart for determining customer cell phone bill
Avoiding Common Errors
in an OR Selection (continued)
28Programming Logic and Design, Eighth Edition
Figure 4-15 Incorrect logic that attempts to provide a discount for young and old movie patrons
Avoiding Common Errors
in an OR Selection (continued)
29Programming Logic and Design, Eighth Edition
Figure 4-16 Correct logic that provides a discount for young and old movie patrons
Avoiding Common Errors
in an OR Selection (continued)
30Programming Logic and Design, Eighth Edition
Figure 4-17 Incorrect logic that attempts to charge full price for patrons whose age is
over 12 and under 65
Avoiding Common Errors
in an OR Selection (continued)
31Programming Logic and Design, Eighth Edition
Figure 4-18 Correct logic that charges full price for patrons whose age is over 12 and under 65
Understanding NOT Logic
• Logical NOT operator
– Reverses the meaning of a Boolean expression
32Programming Logic and Design, Eighth Edition
Table 4-5 Truth table for the NOT operator
if NOT (age < 18) then
output "Can register to vote"
endif
Avoiding a Common Error
in a NOT Expression
• Be careful not to create trivial expressions when using NOT
logic
• Examples (bad and good)
if NOT employeeDept = 1 OR NOT employeeDept = 2 then
output "Employee is not in Department 1 or 2“
Endif
if NOT (employeeDept = 1 OR employeeDept = 2) then
output "Employee is not in Department 1 or 2"
endif
33Programming Logic and Design, Eighth Edition
Making Selections Within Ranges
• Range check
– Compare a variable to a series of values between
limits
• Use the lowest or highest value in each range
• Adjust the question logic when using highest versus
lowest values
• Should end points of the range be included?
– Yes: use >= or <=
– No: use < or >
34Programming Logic and Design, Eighth Edition
Making Selections Within Ranges
(continued)
35Programming Logic and Design, Eighth Edition
Figure 4-19 Discount rates based on items ordered
36Programming Logic and Design, Eighth Edition
Figure 4-20 Flowchart and pseudocode of logic that selects correct discount based on items
Making
Selections
Within
Ranges (continued)
Avoiding Common Errors When
Using Range Checks
• Avoid a dead or unreachable path
–Don’t check for values that can never occur
–Requires some prior knowledge of the data
• Never ask a question if there is only one
possible outcome
• Avoid asking a question when the logic has
already determined the outcome
37Programming Logic and Design, Eighth Edition
38Programming Logic and Design, Eighth Edition
Figure 4-21 Inefficient range selection
including unreachable path
Eliminating Dead
Paths
39Programming Logic and Design, Eighth Edition
Figure 4-22 Inefficient range selection
including unnecessary questions
Avoid Testing the
Same Range Limit
Multiple Times
Understanding Precedence When
Combining AND and OR Operators
• Combine multiple AND and OR operators in an
expression
• When multiple conditions must all be true, use
multiple ANDs
if score1 >= MIN_SCORE AND score2 >=
MIN_SCORE AND score 3 >= MIN_SCORE then
classGrade = "Pass"
else
classGrade = "Fail"
endif
40Programming Logic and Design, Eighth Edition
Understanding Precedence When
Combining AND and OR Operators (continued)
• When only one of multiple conditions must be true,
use multiple ORs
if score1 >= MIN_SCORE OR score2 >=
MIN_SCORE OR score3 >= MIN_SCORE then
classGrade = "Pass"
else
classGrade = "Fail"
endif
41Programming Logic and Design, Eighth Edition
• When AND and OR operators are combined
in the same statement, AND operators are
evaluated first
if age <= 12 OR age >= 65 AND
rating = "G"
• Use parentheses to correct logic and force
evaluations to occur in the order desired
if (age <= 12 OR age >= 65)
AND rating = "G"
42Programming Logic and Design, Eighth Edition
Understanding Precedence When
Combining AND and OR Operators (continued)
• Mixing AND and OR operators makes logic
more complicated
• Can avoid mixing AND and OR decisions
by nesting if statements
43Programming Logic and Design, Eighth Edition
Understanding Precedence When
Combining AND and OR Operators (continued)
44Programming Logic and Design, Eighth Edition
Figure 4-23 Nested decisions that determine movie patron discount
Understanding
Precedence
When
Combining
AND and OR
Operators (continued)
Summary
• Decisions involve evaluating Boolean expressions
• Use relational operators to compare values
• An AND decision requires that both conditions be
true to produce a true result
• In an AND decision, first ask the question that is less
likely to be true
• An OR decision requires that either of the
conditions be true to produce a true result
45Programming Logic and Design, Eighth Edition
Summary (continued)
• In an OR decision, first ask the question that is more
likely to be true
• For a range check:
– Make comparisons with the highest or lowest values in
each range
– Eliminate unnecessary or previously answered questions
• The AND operator takes precedence over the OR
operator
46Programming Logic and Design, Eighth Edition

More Related Content

What's hot

Ian Sommerville, Software Engineering, 9th Edition Ch 4
Ian Sommerville,  Software Engineering, 9th Edition Ch 4Ian Sommerville,  Software Engineering, 9th Edition Ch 4
Ian Sommerville, Software Engineering, 9th Edition Ch 4
Mohammed Romi
 
Ch01 SE
Ch01 SECh01 SE
Ch01 SE
mahirazainab
 
Programming Fundamentals lecture 2
Programming Fundamentals lecture 2Programming Fundamentals lecture 2
Programming Fundamentals lecture 2
REHAN IJAZ
 
Software Engineering Unit 1
Software Engineering Unit 1Software Engineering Unit 1
Software Engineering Unit 1
Abhimanyu Mishra
 
IT8076 - SOFTWARE TESTING
IT8076 - SOFTWARE TESTINGIT8076 - SOFTWARE TESTING
IT8076 - SOFTWARE TESTING
Sathya R
 
Software process
Software processSoftware process
Software process
Jennifer Polack
 
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
IrtazaAfzal3
 
CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2
SIMONTHOMAS S
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
Jesse Manalansan
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2
Namrah Erum
 
Component level design
Component   level designComponent   level design
Component level design
Midhula Chandren
 
Requirements engineering
Requirements engineeringRequirements engineering
Requirements engineering
Syed Zaid Irshad
 
introduction to programming languages
introduction to programming languagesintroduction to programming languages
introduction to programming languages
NaqashAhmad14
 
SE CHAPTER 1 SOFTWARE ENGINEERING
SE CHAPTER 1 SOFTWARE ENGINEERINGSE CHAPTER 1 SOFTWARE ENGINEERING
SE CHAPTER 1 SOFTWARE ENGINEERING
Abrar ali
 
Design concept -Software Engineering
Design concept -Software EngineeringDesign concept -Software Engineering
Design concept -Software Engineering
Varsha Ajith
 
Sdlc models
Sdlc modelsSdlc models
Requirement analysis and specification, software engineering
Requirement analysis and specification, software engineeringRequirement analysis and specification, software engineering
Requirement analysis and specification, software engineering
Rupesh Vaishnav
 
software engineering
 software engineering software engineering
software engineering
Ahmed Elshahat Mohamed
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
mshellman
 
Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
deshpandeamrut
 

What's hot (20)

Ian Sommerville, Software Engineering, 9th Edition Ch 4
Ian Sommerville,  Software Engineering, 9th Edition Ch 4Ian Sommerville,  Software Engineering, 9th Edition Ch 4
Ian Sommerville, Software Engineering, 9th Edition Ch 4
 
Ch01 SE
Ch01 SECh01 SE
Ch01 SE
 
Programming Fundamentals lecture 2
Programming Fundamentals lecture 2Programming Fundamentals lecture 2
Programming Fundamentals lecture 2
 
Software Engineering Unit 1
Software Engineering Unit 1Software Engineering Unit 1
Software Engineering Unit 1
 
IT8076 - SOFTWARE TESTING
IT8076 - SOFTWARE TESTINGIT8076 - SOFTWARE TESTING
IT8076 - SOFTWARE TESTING
 
Software process
Software processSoftware process
Software process
 
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
PRESCRIPTIVE PROCESS MODEL(SOFTWARE ENGINEERING)
 
CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Fundamental Programming Lect 2
Fundamental Programming Lect 2Fundamental Programming Lect 2
Fundamental Programming Lect 2
 
Component level design
Component   level designComponent   level design
Component level design
 
Requirements engineering
Requirements engineeringRequirements engineering
Requirements engineering
 
introduction to programming languages
introduction to programming languagesintroduction to programming languages
introduction to programming languages
 
SE CHAPTER 1 SOFTWARE ENGINEERING
SE CHAPTER 1 SOFTWARE ENGINEERINGSE CHAPTER 1 SOFTWARE ENGINEERING
SE CHAPTER 1 SOFTWARE ENGINEERING
 
Design concept -Software Engineering
Design concept -Software EngineeringDesign concept -Software Engineering
Design concept -Software Engineering
 
Sdlc models
Sdlc modelsSdlc models
Sdlc models
 
Requirement analysis and specification, software engineering
Requirement analysis and specification, software engineeringRequirement analysis and specification, software engineering
Requirement analysis and specification, software engineering
 
software engineering
 software engineering software engineering
software engineering
 
Chapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving ProcessChapter 2 - Beginning the Problem-Solving Process
Chapter 2 - Beginning the Problem-Solving Process
 
Software re engineering
Software re engineeringSoftware re engineering
Software re engineering
 

Viewers also liked

Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
Chaffey College
 
Logic Formulation 3
Logic Formulation 3Logic Formulation 3
Logic Formulation 3
deathful
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
Abed Bukhari
 
Komunikasyon
KomunikasyonKomunikasyon
Komunikasyon
deathful
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
paramalways
 
Minto pyramid
Minto pyramidMinto pyramid
Minto pyramid
shachihp
 

Viewers also liked (6)

Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Logic Formulation 3
Logic Formulation 3Logic Formulation 3
Logic Formulation 3
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Komunikasyon
KomunikasyonKomunikasyon
Komunikasyon
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minto pyramid
Minto pyramidMinto pyramid
Minto pyramid
 

Similar to CIS110 Computer Programming Design Chapter (4)

Csc153 chapter 04
Csc153 chapter 04Csc153 chapter 04
Csc153 chapter 04
PCC
 
LP.ppt
LP.pptLP.ppt
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature SurveyPareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
Abdel Salam Sayyad
 
Lpp through graphical analysis
Lpp through graphical analysis Lpp through graphical analysis
Lpp through graphical analysis
YuktaBansal1
 
QA CHAPTER II.pptx
QA CHAPTER II.pptxQA CHAPTER II.pptx
QA CHAPTER II.pptx
Teshome48
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
ahmed22dg
 
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
Mauro Vallati
 
Cp04invitedslide
Cp04invitedslideCp04invitedslide
Cp04invitedslide
Jean-Francois Puget
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
DanBrown980551
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptx
Chaya64047
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Fdp session rtu session 1
Fdp session rtu session 1Fdp session rtu session 1
Fdp session rtu session 1
sprsingh1
 
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptxUNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
MinilikDerseh1
 
Lecture 5 numbers and built in functions
Lecture 5  numbers and built in functionsLecture 5  numbers and built in functions
Lecture 5 numbers and built in functions
alvin567
 
Database Design Thoughts
Database Design ThoughtsDatabase Design Thoughts
Database Design Thoughts
Koppelaars
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
MLG College of Learning, Inc
 
CNMES15 - Estimation con COSMIC - Alain Abran
CNMES15 - Estimation con COSMIC - Alain AbranCNMES15 - Estimation con COSMIC - Alain Abran
CNMES15 - Estimation con COSMIC - Alain Abran
COSMIC - Common Software Measurement International Consortium
 
problem solving and design By ZAK
problem solving and design By ZAKproblem solving and design By ZAK
problem solving and design By ZAK
Tabsheer Hasan
 
Testcase design techniques final
Testcase design techniques finalTestcase design techniques final
Testcase design techniques final
shraavank
 
PPS_Unit 1.pptx
PPS_Unit 1.pptxPPS_Unit 1.pptx
PPS_Unit 1.pptx
KundanBhatkar
 

Similar to CIS110 Computer Programming Design Chapter (4) (20)

Csc153 chapter 04
Csc153 chapter 04Csc153 chapter 04
Csc153 chapter 04
 
LP.ppt
LP.pptLP.ppt
LP.ppt
 
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature SurveyPareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
Pareto-Optimal Search-Based Software Engineering (POSBSE): A Literature Survey
 
Lpp through graphical analysis
Lpp through graphical analysis Lpp through graphical analysis
Lpp through graphical analysis
 
QA CHAPTER II.pptx
QA CHAPTER II.pptxQA CHAPTER II.pptx
QA CHAPTER II.pptx
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
Argumentation in Artificial Intelligence: From Theory to Practice (Practice)
 
Cp04invitedslide
Cp04invitedslideCp04invitedslide
Cp04invitedslide
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptx
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Fdp session rtu session 1
Fdp session rtu session 1Fdp session rtu session 1
Fdp session rtu session 1
 
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptxUNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
UNIT-2 Quantitaitive Anlaysis for Mgt Decisions.pptx
 
Lecture 5 numbers and built in functions
Lecture 5  numbers and built in functionsLecture 5  numbers and built in functions
Lecture 5 numbers and built in functions
 
Database Design Thoughts
Database Design ThoughtsDatabase Design Thoughts
Database Design Thoughts
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
CNMES15 - Estimation con COSMIC - Alain Abran
CNMES15 - Estimation con COSMIC - Alain AbranCNMES15 - Estimation con COSMIC - Alain Abran
CNMES15 - Estimation con COSMIC - Alain Abran
 
problem solving and design By ZAK
problem solving and design By ZAKproblem solving and design By ZAK
problem solving and design By ZAK
 
Testcase design techniques final
Testcase design techniques finalTestcase design techniques final
Testcase design techniques final
 
PPS_Unit 1.pptx
PPS_Unit 1.pptxPPS_Unit 1.pptx
PPS_Unit 1.pptx
 

More from Dr. Ahmed Al Zaidy

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
Dr. Ahmed Al Zaidy
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
Dr. Ahmed Al Zaidy
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheets
Dr. Ahmed Al Zaidy
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and Styles
Dr. Ahmed Al Zaidy
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
Dr. Ahmed Al Zaidy
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
Dr. Ahmed Al Zaidy
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
Dr. Ahmed Al Zaidy
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
Dr. Ahmed Al Zaidy
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
Dr. Ahmed Al Zaidy
 
Chapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile webChapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile web
Dr. Ahmed Al Zaidy
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
Dr. Ahmed Al Zaidy
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page Layout
Dr. Ahmed Al Zaidy
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSS
Dr. Ahmed Al Zaidy
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5
Dr. Ahmed Al Zaidy
 
Integer overflows
Integer overflowsInteger overflows
Integer overflows
Dr. Ahmed Al Zaidy
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
Dr. Ahmed Al Zaidy
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
Dr. Ahmed Al Zaidy
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
Dr. Ahmed Al Zaidy
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
Dr. Ahmed Al Zaidy
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
Dr. Ahmed Al Zaidy
 

More from Dr. Ahmed Al Zaidy (20)

Chapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based ProgrammingChapter 14 Exploring Object-based Programming
Chapter 14 Exploring Object-based Programming
 
Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
 
Chapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheetsChapter 12 Working with Document nodes and style sheets
Chapter 12 Working with Document nodes and style sheets
 
Chapter 11 Working with Events and Styles
Chapter 11 Working with Events and StylesChapter 11 Working with Events and Styles
Chapter 11 Working with Events and Styles
 
Chapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statementsChapter 10 Exploring arrays, loops, and conditional statements
Chapter 10 Exploring arrays, loops, and conditional statements
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
 
Chapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimediaChapter 8 Enhancing a website with multimedia
Chapter 8 Enhancing a website with multimedia
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
 
Chapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and ColumnsChapter 6 Working with Tables and Columns
Chapter 6 Working with Tables and Columns
 
Chapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile webChapter 5 Designing for the mobile web
Chapter 5 Designing for the mobile web
 
Chapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSSChapter 4 Graphic Design with CSS
Chapter 4 Graphic Design with CSS
 
Chapter 3 Designing a Page Layout
Chapter 3 Designing a Page LayoutChapter 3 Designing a Page Layout
Chapter 3 Designing a Page Layout
 
Chapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSSChapter 2 Getting Started with CSS
Chapter 2 Getting Started with CSS
 
Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5Chapter 1 Getting Started with HTML5
Chapter 1 Getting Started with HTML5
 
Integer overflows
Integer overflowsInteger overflows
Integer overflows
 
testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2testing throughout-the-software-life-cycle-section-2
testing throughout-the-software-life-cycle-section-2
 
Fundamental of testing
Fundamental of testingFundamental of testing
Fundamental of testing
 
Chapter 15 Risk Mitigation
Chapter 15 Risk MitigationChapter 15 Risk Mitigation
Chapter 15 Risk Mitigation
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data SecurityChapter 13 Vulnerability Assessment and Data Security
Chapter 13 Vulnerability Assessment and Data Security
 

Recently uploaded

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 

CIS110 Computer Programming Design Chapter (4)

  • 1. Programming Logic and Design Eighth Edition Chapter 4 Making Decisions
  • 2. Objectives In this chapter, you will learn about: • Boolean expressions and the selection structure • The relational comparison operators • AND logic • OR logic • NOT logic • Making selections within ranges • Precedence when combining AND and OR operators 2Programming Logic and Design, Eighth Edition
  • 3. Boolean Expressions and the Selection Structure • Boolean expressions can be only true or false • Every computer decision yields a true-or-false, yes-or-no, 1-or-0 result • Used in every selection structure 3Programming Logic and Design, Eighth Edition
  • 4. Boolean Expressions and the Selection Structure (continued) • Dual-alternative (or binary) selection structure – Provides an action for each of two possible outcomes 4Programming Logic and Design, Eighth Edition Figure 4-1 The dual-alternative selection structure
  • 5. Boolean Expressions and the Selection Structure (continued) • Single-alternative (or unary) selection structure – Action is provided for only one outcome – if-then 5Programming Logic and Design, Eighth Edition Figure 4-2 The single-alternative selection structure
  • 6. 6Programming Logic and Design, Eighth Edition Figure 4-3 Flowchart and pseudocode for overtime payroll program
  • 7. Boolean Expressions and the Selection Structure (continued) • if-then-else decision –if-then clause • Holds the action or actions that execute when the tested condition in the decision is true –else clause • Executes only when the tested condition in the decision is false 7Programming Logic and Design, Eighth Edition
  • 8. Using Relational Comparison Operators • Relational comparison operators – Six types supported by all modern programming languages – Two values compared can be either variables or constants • Trivial expressions – Will always evaluate to the same result – Examples: • 20 = 20? TRUE • 30 = 40? FALSE 8Programming Logic and Design, Eighth Edition
  • 9. 9Programming Logic and Design, Eighth Edition Table 4-2 Relational comparison operators
  • 10. Using Relational Comparison Operators (continued) • Any decision can be made with only three types of comparisons: =, >, and < • “Not equal” operator <> –Involves thinking in double negatives –Best to restrict usage to “if without an else”— that is, only take action when some comparison is false 10Programming Logic and Design, Eighth Edition
  • 11. Using Relational Comparison Operators (continued) 11Programming Logic and Design, Eighth Edition Figure 4-5 Using a negative comparison
  • 12. Using Relational Comparison Operators (continued) 12Programming Logic and Design, Eighth Edition Figure 4-6 Using the positive equivalent of the negative comparison in Figure 4-5
  • 13. Avoiding a Common Error with Relational Operators • Common errors –Using the wrong operator •BIG > small •small < BIG –Missing the boundary or limit required for a selection 13Programming Logic and Design, Eighth Edition
  • 14. Understanding AND Logic • Compound condition –Asks multiple questions before an outcome is determined • AND decision –Requires that both of two tests evaluate to true –Requires a nested decision (nested if) or a cascading if statement 14Programming Logic and Design, Eighth Edition
  • 15. 15Programming Logic and Design, Eighth Edition Figure 4-7 Flowchart and pseudocode for cell phone billing program
  • 16. Nesting AND Decisions for Efficiency • When nesting decisions – Either selection can come first • Performance time can be improved by asking questions in the proper order • In an AND decision, first ask the question that is less likely to be true – Eliminates as many instances of the second decision as possible – Speeds up processing time 16Programming Logic and Design, Eighth Edition
  • 17. Using the AND Operator • Conditional AND operator – Ask two or more questions in a single comparison – Each Boolean expression must be true for entire expression to evaluate to true • Truth tables – Describe the truth of an entire expression based on the truth of its parts • Short-circuit evaluation – Expression evaluated only as far as necessary to determine truth 17Programming Logic and Design, Eighth Edition
  • 18. Using the AND Operator (continued) 18Programming Logic and Design, Eighth Edition Table 4-3 Truth table for the AND operator
  • 19. 19Programming Logic and Design, Eighth Edition Figure 4-9 Using an AND operator and the logic behind it Using the AND Operator (continued)
  • 20. Avoiding Common Errors in an AND Selection • Second decision must be made entirely within the first decision • In most programming languages, logical AND is a binary operator – Requires a complete Boolean expression on both sides 20Programming Logic and Design, Eighth Edition
  • 21. Understanding OR Logic • OR decision –Take action when one or the other of two conditions is true –Example • “Are you free for dinner Friday or Saturday?” 21Programming Logic and Design, Eighth Edition
  • 22. Writing OR Selections for Efficiency • May ask either question first –Both produce the same output but vary widely in number of questions asked • If first question is true, no need to ask second • In an OR decision, first ask the question that is more likely to be true –Eliminate as many extra decisions as possible 22Programming Logic and Design, Eighth Edition
  • 23. Using the OR Operator • Conditional OR operator – Ask two or more questions in a single comparison • Only one Boolean expression in an OR selection must be true to produce a result of true • Question placed first will be asked first – Consider efficiency • Computer can ask only one question at a time 23Programming Logic and Design, Eighth Edition
  • 24. Using the OR Operator (continued) 24Programming Logic and Design, Eighth Edition Table 4-4 Truth table for the OR operator
  • 25. 25Programming Logic and Design, Eighth Edition Figure 4-13 Using an OR operator and the logic behind it Using the OR Operator (continued)
  • 26. Avoiding Common Errors in an OR Selection • Second question must be a self-contained structure with one entry and exit point • Request for A and B in English logically means a request for A or B – Examples • “Add $20 to the bill of anyone who makes more than 100 calls and to anyone who has used more than 500 minutes” • “Add $20 to the bill of anyone who has made more than 100 calls or has used more than 500 minutes” 26Programming Logic and Design, Eighth Edition
  • 27. Avoiding Common Errors in an OR Selection (continued) 27Programming Logic and Design, Eighth Edition Figure 4-14 Unstructured flowchart for determining customer cell phone bill
  • 28. Avoiding Common Errors in an OR Selection (continued) 28Programming Logic and Design, Eighth Edition Figure 4-15 Incorrect logic that attempts to provide a discount for young and old movie patrons
  • 29. Avoiding Common Errors in an OR Selection (continued) 29Programming Logic and Design, Eighth Edition Figure 4-16 Correct logic that provides a discount for young and old movie patrons
  • 30. Avoiding Common Errors in an OR Selection (continued) 30Programming Logic and Design, Eighth Edition Figure 4-17 Incorrect logic that attempts to charge full price for patrons whose age is over 12 and under 65
  • 31. Avoiding Common Errors in an OR Selection (continued) 31Programming Logic and Design, Eighth Edition Figure 4-18 Correct logic that charges full price for patrons whose age is over 12 and under 65
  • 32. Understanding NOT Logic • Logical NOT operator – Reverses the meaning of a Boolean expression 32Programming Logic and Design, Eighth Edition Table 4-5 Truth table for the NOT operator if NOT (age < 18) then output "Can register to vote" endif
  • 33. Avoiding a Common Error in a NOT Expression • Be careful not to create trivial expressions when using NOT logic • Examples (bad and good) if NOT employeeDept = 1 OR NOT employeeDept = 2 then output "Employee is not in Department 1 or 2“ Endif if NOT (employeeDept = 1 OR employeeDept = 2) then output "Employee is not in Department 1 or 2" endif 33Programming Logic and Design, Eighth Edition
  • 34. Making Selections Within Ranges • Range check – Compare a variable to a series of values between limits • Use the lowest or highest value in each range • Adjust the question logic when using highest versus lowest values • Should end points of the range be included? – Yes: use >= or <= – No: use < or > 34Programming Logic and Design, Eighth Edition
  • 35. Making Selections Within Ranges (continued) 35Programming Logic and Design, Eighth Edition Figure 4-19 Discount rates based on items ordered
  • 36. 36Programming Logic and Design, Eighth Edition Figure 4-20 Flowchart and pseudocode of logic that selects correct discount based on items Making Selections Within Ranges (continued)
  • 37. Avoiding Common Errors When Using Range Checks • Avoid a dead or unreachable path –Don’t check for values that can never occur –Requires some prior knowledge of the data • Never ask a question if there is only one possible outcome • Avoid asking a question when the logic has already determined the outcome 37Programming Logic and Design, Eighth Edition
  • 38. 38Programming Logic and Design, Eighth Edition Figure 4-21 Inefficient range selection including unreachable path Eliminating Dead Paths
  • 39. 39Programming Logic and Design, Eighth Edition Figure 4-22 Inefficient range selection including unnecessary questions Avoid Testing the Same Range Limit Multiple Times
  • 40. Understanding Precedence When Combining AND and OR Operators • Combine multiple AND and OR operators in an expression • When multiple conditions must all be true, use multiple ANDs if score1 >= MIN_SCORE AND score2 >= MIN_SCORE AND score 3 >= MIN_SCORE then classGrade = "Pass" else classGrade = "Fail" endif 40Programming Logic and Design, Eighth Edition
  • 41. Understanding Precedence When Combining AND and OR Operators (continued) • When only one of multiple conditions must be true, use multiple ORs if score1 >= MIN_SCORE OR score2 >= MIN_SCORE OR score3 >= MIN_SCORE then classGrade = "Pass" else classGrade = "Fail" endif 41Programming Logic and Design, Eighth Edition
  • 42. • When AND and OR operators are combined in the same statement, AND operators are evaluated first if age <= 12 OR age >= 65 AND rating = "G" • Use parentheses to correct logic and force evaluations to occur in the order desired if (age <= 12 OR age >= 65) AND rating = "G" 42Programming Logic and Design, Eighth Edition Understanding Precedence When Combining AND and OR Operators (continued)
  • 43. • Mixing AND and OR operators makes logic more complicated • Can avoid mixing AND and OR decisions by nesting if statements 43Programming Logic and Design, Eighth Edition Understanding Precedence When Combining AND and OR Operators (continued)
  • 44. 44Programming Logic and Design, Eighth Edition Figure 4-23 Nested decisions that determine movie patron discount Understanding Precedence When Combining AND and OR Operators (continued)
  • 45. Summary • Decisions involve evaluating Boolean expressions • Use relational operators to compare values • An AND decision requires that both conditions be true to produce a true result • In an AND decision, first ask the question that is less likely to be true • An OR decision requires that either of the conditions be true to produce a true result 45Programming Logic and Design, Eighth Edition
  • 46. Summary (continued) • In an OR decision, first ask the question that is more likely to be true • For a range check: – Make comparisons with the highest or lowest values in each range – Eliminate unnecessary or previously answered questions • The AND operator takes precedence over the OR operator 46Programming Logic and Design, Eighth Edition