Download presentation
Presentation is loading. Please wait.
Published byAnnis Powell Modified over 9 years ago
1
Advanced Logic Copyright © 1999 Patrick McDermott College of Alameda pmcdermott@peralta.edu
2
What’s a Statement? The Statement Below is true. The Statement Above is false. No Fuzzy Logic: Every Statement is either true 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?
3
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
4
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 ≡ 0 true ≡ !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.
5
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
6
Exclusive OR The 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
7
Words Can Trick You Or 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 and if, if is and if (a) if (b) if (a && b) No maybe s NOT might not mean not It’s not about the money It’s about the money Flammable == 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.
8
? : Conditional Operator “Ternary Conditional” // First, we do it with an if if(OrderTotal < 100.00) ShippingCharge = 0.10 * OrderTotal; else ShippingCharge = 0.00; // The Same thing with a conditional cout << "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
9
return to me Anything that return s 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 ? :
10
&= &= ^= |= Are All Rich People Happy? bool Happy = true; bool Rich = false; Happy |= Rich; Same as Happy = Happy | Rich; –Now All Rich People are Happy
11
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
12
Truth Table
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.