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

Post on 05-Jan-2016

214 views 0 download

transcript

Advanced LogicCopyright © 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?

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

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.

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

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

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.

? : 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

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 ? :

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

– Now All Rich People are Happy

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

Truth Table

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