Booleans - Lecture 26

Summary Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes Some Boolean algebra

ppt48 trang | Chia sẻ: thucuc2301 | Lượt xem: 563 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Booleans - Lecture 26, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Booleans Lecture 26Summary of previous lectureIn the previous lecture, we have been learnt,The if statementThe else statementCascaded ifNested ifSwitch statementToday’s TopicsBooleanType int as BooleanBoolean expressionsBoolean OperatorsPrecedenceCommon mistakesBooleanA special “type” with only two values: false and true.Used to implement conditions for selection and looping in an algorithmBoolean expressions represent statements which are either strictly true or strictly falseWe still have to coverBoolean Algebra --- HistoryGeorge Boole, 1815-1864Initially self-guided studies of languages and philosophyWith 16 assistant teacher at private school With 20 opened a school and taught himself mathematicsPublished in “Cambridge Mathematical Journal” with 24The Mathematical Analysis of Logic was published in 1847Casts logical reasoning in the form of algebra+ is OR, * is AND0 is FALSE, 1 is TRUEAn Investigation of the Laws of Thought (1854) extends the algebraType int as BooleanIn C, integers are used as BooleansInteger value 0 is false.Any non-zero integer value is true.#include /* Test some Booleans. */int main(){ int whatever = 0; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Example: What is the output?#include /* Test some Booleans. */int main(){ int whatever = 1; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Example: What is the output?#include /* Test some Booleans. */int main(){ int whatever = -100; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Example: What is the output?#include /* Test some Booleans. */int main(){ int whatever = ’A’; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Example: What is the output?#include /* Test some Booleans. */int main(){ int whatever = 0.003; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Example: What is the output?Example: What is the output?#include /* Test some Booleans. */int main(){ float whatever = 0.003; if (whatever) { printf(“Is that true, John?\n”); } else { printf(“No, Jane.\n”); } return 0;}Behavior is “undefined.”Boolean Expressions...are either strictly true or strictly false.... can be evaluated to determine their true or falsehood.A Boolean expression which evaluates to true has integer value 1; otherwise, 0.Remember: any non-zero value is taken interpreted as trueBoolean Operators...are used in forming Boolean expressions....are also known as “logical” operatorsAnd (&&)Or (||)Not (!)Equality (==)Inequality (!=)Comparison (, =)AndTrue only if both Boolean arguments are true.1 && 21 && 0 && -1healthy && wealthy && wiseExamples:AndTrue only if both Boolean arguments are true.1 && 21 && 0 && -1healthy && wealthy && wiseExamples:not to be confused with “bitwise AND” (&)OrTrue if either Boolean argument is true (or both are true).1 || 211 || 0 || 1good || bad || uglyExamples:OrTrue only if either Boolean argument is true (or both are true).1 || 211 || 0 || 1good || bad || uglyExamples:not to be confused with “bitwise OR” (|)NotTrue only if the single Boolean argument is false.! 1! 0! happyExamples:Reminder: GatesAB00011011AND GateA AND BA OR BOR GateaddmultA01NOT ANOT GateReminder: GatesEqualityTrue only if both arguments have identical values.1 == 21 == 042 == 42truth == beautyExamples:EqualityTrue only if both arguments have identical values.1 == 21 == 042 == 42truth == beautyExamples:not to be confused with assignment (=)InequalityTrue only if arguments have different values.1 != 21 != 042 != 42truth != beautyExamples:ComparisonTrue only if the specified relationship holds1 142 = 18Examples:Short-circuitingA complex Boolean expression is only evaluated as far as necessary1 || 20 || 1 || 21 && 0 && -1Examples:PrecedenceHighest to lowest:BracketsNot (!) Comparison (, =)Equality (==) Inequality (!=)And (&&)Or (||)Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators. Boolean AlgebraNOT false = trueNOT true = falsetrue AND true = truetrue AND false = falsetrue OR true = truetrue OR false = truefalse AND false = falsefalse OR false = falseif (0 || 0) printf("Does false OR false = true?");if (0 && 0) printf("Does false AND false = true?");Example:#include int main(){ int age = 18; int haveMoney = 0; int haveCard = 1; float thirst = 0.31; int afterHours = 1; int result; result = age >= 18 && (haveMoney || haveCard) && thirst > 0.3 && ! afterHours; printf("%d\n", result); return 0;}bool.cCommon MistakesUsing = instead of ==Example:#include /* Probably the most common C error. */int main(){ int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0;}boolerr1.cExample:#include /* Probably the most common C error. */int main(){ int score; scanf("%d", &score); if (score = 48 || score = 49) { printf("Almost!\n"); } return 0;}boolerr1.cUsual error message (if any): “LValue required...”Example:#include /* Probably the most common C error. */int main(){ int score; scanf("%d", &score); if (score == 48 || score == 49) { printf("Almost!\n"); } return 0;}boolerr1.cCommon MistakesUsing = instead of ==Multiple comparisonsExample:#include /* Another common C error. */int main(){ int score; scanf("%d", &score); if ( 0 /* Another common C error. */int main(){ int score; scanf("%d", &score); if ( 0 /* Another common C error. */int main(){ int score; scanf("%d", &score); if ( 0 /* Another common C error. */int main(){ int score; scanf("%d", &score); if ( 0 b) ? a : b ;More Examplesch1 = 'a';ch2 = 'a';printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1ch1 = 'd';ch2 = 'f';printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1ch1 = 'a';ch2 = '\0';printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0 SummaryBooleanType int as BooleanBoolean expressionsBoolean OperatorsPrecedenceCommon mistakesSome Boolean algebra

Các file đính kèm theo tài liệu này:

  • pptlecture_26_74_2027004.ppt
Tài liệu liên quan