Top Banner
Boolean Tricks
27

Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Jan 03, 2016

Download

Documents

Ariel Simmons
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Boolean Tricks

Page 2: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Expressions in Conditions

• Condition must evaluate to Boolean but can be arbitrarily complex

Page 3: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Expressions in Conditions

• Condition must evaluate to Boolean but can be arbitrarily complex

• Just because you can doesn't mean you should. Better:

Page 4: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

%

• Check if x is a multiple of y:x % y == 0

Page 5: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Even/Odd

• x % 2 is:– 0 even– 1 odd

Page 6: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Comparing Floating Points

• According to C++:3.02 != (3 + .01 + .01)

• Do NOT compare floats directly with == or !=

Page 7: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Comparing Floating Points

• Compare if two floating points are "close enough"|num1 – num2| < threshold

Page 8: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Comparing Floating Points

• Compare if two floating points are "close enough"|num1 – num2| < threshold

• absolute value: abs(expression) –in <cmath>

• threshold–depends on problem, have 15 significant digits to work with

Page 9: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Comparing Floating Points

Page 10: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Naked Booleans

• Boolean variable can be used as condition:

• Same as:

Page 11: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

!

• ! is "not"

• Preferred to:

Page 12: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Problem

• Want to read in temperature and print "Temp is good" if it is between 68 and 72, or warning if it is too hot/cold.

Page 13: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Boolean Flags

• Flag variable : Boolean used to track condition– Start as true or false– Modify based on eveidence

Page 14: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Nested Ifs

Page 15: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Nested ifs

• Conditionals can be "nested"

Page 16: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else Matching

• This code has a logic error:

Page 17: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else Matching

• This code has a logic error:

Page 18: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else Matching

• What computer saw:

Page 19: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else Matching

• { } make ending of if/else explicit

Editor will show brace matching

Use indentation as visual clue

Page 20: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else

• If can be the body of an else:

Page 21: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

If/Else

• if… else if… else if… else form:– More compact/legible– Only one option is chosen

Page 22: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Bad If/Else

• A chain of plain if's not mutually exclusive!

Page 23: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Example: Computing TaxesThe US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household.

Read in someone's status and income and determine their tax:

Page 24: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Conditions

• 2 Inputs– Marital status– $ earned

• 1 Output: tax

Page 25: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

What happens?

• No matter what:– Get status– Get income– Print tax

• Conditionally:– What math to do

Page 26: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Step 1

• Distinguish based on status

Page 27: Boolean Tricks. Expressions in Conditions Condition must evaluate to Boolean but can be arbitrarily complex.

Step 2

• Refine each category based on earnings: