Top Banner
Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda [email protected]
12

Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda [email protected].

Jan 05, 2016

Download

Documents

Annis Powell
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: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Advanced LogicCopyright © 1999 Patrick McDermott

College of Alameda

[email protected]

Page 2: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

What’s a Statement?

The Statement Below is true.

The Statement Above is false.

No Fuzzy Logic: Every Statement is eithertrue or it is false.There is no maybe.

“The Law of the Excluded Middle”

Bertrand Russell’s Principia Mathematica Nightmare:“The current King of France is bald.”

“The current King of France has a luxurious mane.”Can 2 opposites both be false?

Page 3: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Logical Precedence()++ -- !* / %+ -<< >> [bitwise shift, overridden as stream operator (yuck!)] < <= > >=== != []&& [Boolean Product ×]|| [Boolean Sum +]? : [Conditional]= += -= *= /= [Assignment operators], [comma]

The simple rule:put parentheses

in cout around =Conditionals,

Logic

Page 4: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Subtle Difference C++ vs C#• bool Truly a Type in C#• Added to C++ Later

– Actually an int with values 0 and 1– if(c--){…}; while(S[i]){…};

false ≡ 0true ≡ !false

– It’s true if it’s not false.• maybe is therefore impossible.

“Pat” is true.3,329,432 is true.Any Negative Number is true.

Page 5: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

AND vs. OR

p && q && r && s && t && u– true only when ALL are true– false if any one is false

p || q || r || s || t || u– false only when ALL are false– true if any one is true

Short Circuit– C++: Stops evaluating when determined– C#: | and & fully evaluate, || and && short-

circuit

Page 6: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Exclusive ORThe construct and/or is especially awkward. In

general, where it is important to mark an inclusive or, use “x or y, or both”, rather than “x and/or y”. For an exclusive or, use “either x or y”, and optionally add “but not both”, if it is necessary to stress the exclusivity.

Where there are more than two possibilities presented, from which some combination is to be selected, it is even less desirable to use and/or. With two possibilities, at least the intention is clear; but with more than two it may not be determinate (see The Cambridge Guide to English Usage, 2004, p. 38). Instead of “x, y, and/or z”, use an appropriate alternative: “one or more of x, y, and z”; “some or all of x, y, and z”; etc. — Wikipedia Manual of Style

Page 7: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Words Can Trick YouOr can mean &&, and and can mean ||

Students with As and Bs if (Grade == 'A' || Grade == 'B')“Choose between X and Y” means “Choose X or Y”

But is logically just an andif, if is and

if (a) if (b) if (a && b)

No maybesNOT might not mean not

It’s not about the money It’s about the moneyFlammable == Inflammable; Regardless == Irregardless

No shortcuts (Can’t distribute test)Æif (Grade == 'A' || 'B'): a Tautology

Tautology: A statement that is always true by its nature.

Page 8: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

? : Conditional Operator

“Ternary Conditional”

// First, we do it with an ifif(OrderTotal < 100.00)

ShippingCharge = 0.10 * OrderTotal;else

ShippingCharge = 0.00;

// The Same thing with a conditionalcout << "The if statement says the charge will be "

<< ShippingCharge << ".\n“

<< "The conditional operator is more succinct: " << (OrderTotal < 100.00? 0.10 * OrderTotal : 0.00) << ".\n"; // No variable needed

Page 9: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

return to me• Anything that returns a type can take the

place of that type• Everyone gets a $1,000 Bonus Salary Grades

above Grade 6 also get an additional $100 bonus for each grade

double Bonus=1000.0+(SalaryGrade>6?100.0*SalaryGrade:0.0);

// Same thing, spread out:double Bonus = 1000.0 +

(SalaryGrade > 6? 100.0 * SalaryGrade :0.0);

Need () because + is higher on precedence chart than ? :

Page 10: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

&=• &= ^= |=• Are All Rich People Happy?• bool Happy = true;• bool Rich = false;• Happy |= Rich;• Same as Happy = Happy | Rich;

– Now All Rich People are Happy

Page 11: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

de Morgan

!(p && q) !p || !q

!(p || q) !p && !q

Augustus de Morgan, 1806-1871

cf. Algebra: -(+x + y) -x - y -(+x × +y) -x × -y

Boolean Algebra: -(+p + q) -p × -q -(+p × +q) -p - q

Page 12: Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu.

Truth Table

p q p ☺ qtrue true t/ftrue false t/ffalse true t/ffalse false t/f