neal stublen [email protected]. human decisions think about a decision you've made today? how...
TRANSCRIPT
![Page 2: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/2.jpg)
MAKING DECISIONS
![Page 3: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/3.jpg)
Human Decisions
Think about a decision you've made today?
How did you arrive at that decision? Was it a black-and-white decision for
you? Our decisions can be complex?
![Page 4: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/4.jpg)
Computer "Decisions"
Computer decisions always reduce down to simple logic comparisons
Something is true or something is false It has a boolean value of true or false The decision block in a flow diagram
requires a yes/no decisionIs the light red?Is the light yellow?Is the light green?
![Page 5: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/5.jpg)
Boolean Decisions
We use boolean expressions to determine a true or false condition
We implement a selection structure Dual-alternative selection (if-then-else) Single-alternative selection (if-then)
![Page 6: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/6.jpg)
If-Then If the condition is true, then perform an action
or a sequence of actions
if condition then
action
endif
if time is 6pm then
begin class
endif
![Page 7: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/7.jpg)
If-Then-Else If the condition is true, then perform an action or a sequence of
actions Else perform a different action or sequence of actions
if condition then
action
else
another action
endif
if time is 6pm then
begin class
else
wait
endif
![Page 8: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/8.jpg)
Boolean Expressions
An expression is just a statement the computer will evaluate4 + 3x / 4
A boolean expression will be evaluated as either true or false
Relational operators are used in boolean expressions
![Page 9: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/9.jpg)
Relational Operators
Equivalence, = or == Greater than, > Less than, < Greater than or equal to, >= Less than or equal to, <= Not equal to, <> or !=
Also called comparison operators
![Page 10: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/10.jpg)
Relational Operators
if customerAge >= 65 then
discount = 0.10
else
discount= 0.0
endif
if customerAge <65 then
discount = 0.0
else
discount = 0.10
endif
![Page 11: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/11.jpg)
Negation Operator A negation operator reverses the logic of a
true or false expression (NOT or !)
if age >= 21 then
allow purchase
endif
if NOT age >= 21 then
refuse purchase
endif
![Page 12: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/12.jpg)
Example
What logic can we use to implement the following discount table:
Purchase Discount
$0.00 to $25.00 5%
$25.01 to $50.00 10%
$50.01 to $100.00 15%
Over $100.00 20%
![Page 13: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/13.jpg)
Implementing Minimal Conditions Selecting from a group of ranges, the
last check is never necessary If the table is complete, we rule out one
possibility with each check If total is not > 100 and total is not > 50
and total is not > 25, then total must be <= 25.
![Page 14: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/14.jpg)
Compound Conditions
Sometimes we need more complex logic to make a decision
if I’m speeding then
if I see a police car then
slow down immediately
endif
endif
![Page 15: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/15.jpg)
Nested Decisions
if condition1 then
if condition2 then
take action
endif
endif
if condition1 AND condition2 then
take action
endif
![Page 16: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/16.jpg)
AND Operatorif x AND y then
do something
endif
“x AND y” is a boolean expression that can be evaluated as true or false
x y x AND y
True True True
True False False
False True False
False False False
![Page 17: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/17.jpg)
Short-Circuit Evaluation
if x AND y then
do something
endif
If we know x is false, we know x AND y is also false.
There’s no need to evaluate y.
![Page 18: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/18.jpg)
Short-Circuit Example
if age > 12 AND age < 65 then
movieDiscount = 0.0
endif
If age is less than or equal to twelve, the computer will not need to determine if age is greater than 65.
![Page 19: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/19.jpg)
Example
How would we implement the following discount table:
On Sundays, senior citizens receive an additional 5% discount.
Purchase Discount
$0.00 to $25.00 5%
$25.01 to $50.00 10%
$50.01 to $100.00 15%
Over $100.00 20%
![Page 20: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/20.jpg)
OR Operatorif x OR y then
do something
endif
“x OR y” is a boolean expression that can be evaluated as true or false
x y x OR y
True True True
True False True
False True True
False False False
![Page 21: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/21.jpg)
Short-Circuit Evaluation?
if x OR y then
do something
endif
Is there a short-circuit evaluation for the OR operator?
If we know x is true, we know x OR y is also true.
There’s no need to evaluate y.
![Page 22: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/22.jpg)
OR Efficiency
if age < 12 then
movieDiscount = 0.10
endif
if age > 65 then
movieDiscount = 0.10
endif
if age < 12 OR age > 65 then
movieDiscount = 0.10
endif
![Page 23: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/23.jpg)
AND/OR Precedence
AND operator is always evaluated before the OR operator
c1 OR c2 AND c3 OR c4 c1 OR (c2 AND c3) OR c4
![Page 24: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/24.jpg)
Precedence Example
if age <=12 OR age >= 65 AND not Friday then
discount = 0.10
endif
A twelve year old still gets the discount on Friday.
if (age <=12 OR age >= 65) AND not Friday then
discount = 0.10
endif
Parentheses always clarify intention.
![Page 25: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/25.jpg)
Summary
Boolean expressions Relational operators AND Logic OR Logic Selection within ranges AND/OR precedence
![Page 26: Neal Stublen nstublen@jccc.edu. Human Decisions Think about a decision you've made today? How did you arrive at that decision? Was it a black-and-white](https://reader035.vdocuments.mx/reader035/viewer/2022062621/551c0afa550346ad4f8b50e5/html5/thumbnails/26.jpg)
Date Validation
What logic would we need to validate a user’s date input?
The user enters separate values for the month, day, and year.