chapter 4: selection control structure. objectives use the relational comparison operators learn...
TRANSCRIPT
![Page 1: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/1.jpg)
CHAPTER 4:Selection Control Structure
![Page 2: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/2.jpg)
Objectives
Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections within ranges Learn about precedence when combining
AND and OR selections Learn more about the case structure Use a decision table
![Page 3: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/3.jpg)
Comparisons Boolean expression
Represents only one of two states Evaluates to true or false
Every decision in a computer program involves evaluating a Boolean expression
Computer circuitry consists of two-state on-off switches Represented by 1 or 0
Every computer decision yields a true-false result
![Page 4: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/4.jpg)
Relational Operators Any logical situation can be expressed
with only three types of comparisons: =, >, and <– Operators >= and <= are not necessary, but
make code more readable “Not equal” operator
Most confusing of comparisons Most likely to be different in different
languages
![Page 5: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/5.jpg)
Example
![Page 6: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/6.jpg)
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)
Using nested if statements– Second selection structure is contained entirely
within one side of first structure– else clause paired with last if
![Page 7: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/7.jpg)
Truth table for the AND operator
Understanding AND Logic
![Page 8: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/8.jpg)
Combining Decisions in an AND Selection (continued)
Figure 4-10 Using an AND operator and the logic behind it
![Page 9: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/9.jpg)
Avoiding Common Errors in an AND Selection Second decision must be made entirely
within the first decision Range of values – every value between
low and high limits In most programming languages
logical AND is a binary operator Requires complete Boolean expression on
both sides
![Page 10: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/10.jpg)
Understanding OR Logic
When you want to take action when one or the other of two conditions is true
Example: Salespeople get bonus when they have
achieved one of two goals: Sell at least five items Sell at least $2,000 in merchandise
– itemsSold >= ITEMS_MIN? If true, assign $300 bonus
![Page 11: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/11.jpg)
Writing OR Decisions 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 Eliminates as many repetitions as possible
of second decision
![Page 12: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/12.jpg)
Combining Decisions in an OR Selection
Conditional OR operator allows you to 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, so consider efficiency
Computer can ask only one question at a time
![Page 13: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/13.jpg)
Combining Decisions in an OR Selection (continued)
Truth table for the OR operator
![Page 14: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/14.jpg)
Combining Decisions in an OR Selection (continued)
![Page 15: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/15.jpg)
Avoiding Common Errors in an OR Selection
Second question must be self-contained structure with one entry and exit point
Request for A and B in English often translates to a request for A or B logically Example:
“Give a bonus to anyone who has sold at least three items and to anyone who has sold $2000”
“Give a bonus to anyone who has sold at least three items or $2000”
![Page 16: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/16.jpg)
Understanding Precedence When Combining AND and OR Selections
Combine multiple AND and OR operators in an expression When multiple conditions must all be true, use multiple ANDs
if score1 >= 75 AND score2 >= 75 AND score 3 >= 75 then
classGrade = “Pass”
else
classGrade = “Fail”
endif
![Page 17: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/17.jpg)
Understanding Precedence When Combining AND and OR Selections
When only one of multiple conditions must be true, use multiple ORs
if score1 >= 75 OR score2 >= 75 OR score3 >= 75 then
classGrade = “Pass”
else
classGrade = “Fail”
endif
![Page 18: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/18.jpg)
Understanding Precedence When Combining AND and OR Selections
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”
![Page 19: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/19.jpg)
Understanding Precedence When Combining AND and OR Selections (continued) Mixing AND and OR operators makes
logic more complicated Can avoid mixing AND and OR
decisions by nesting if statements
![Page 20: CHAPTER 4: Selection Control Structure. Objectives Use the relational comparison operators Learn about AND logic Learn about OR logic Make selections](https://reader035.vdocuments.site/reader035/viewer/2022070410/56649f0c5503460f94c1f135/html5/thumbnails/20.jpg)
Understanding Precedence When Combining AND and OR Selections
Nested decisions that determine movie patron discount