Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.

Similar presentations


Presentation on theme: "1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver."— Presentation transcript:

1 1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver

2 2 Topics Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

3 3 Boolean A special “type” with only two values: –false and true. Used for conditions –for selection and looping

4 4 Type int as Boolean In C, integers are used as Booleans Integer value 0 is false. Any non-zero integer value is true.

5 5 #include /* Test some Booleans. */ int main() { int whatever = 0; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

6 6 #include /* Test some Booleans. */ int main() { int whatever = 1; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

7 7 #include /* Test some Booleans. */ int main() { int whatever = -100; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

8 8 #include /* Test some Booleans. */ int main() { int whatever = ’A’; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

9 9 #include /* Test some Booleans. */ int main() { int whatever = 0.003; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Example: What is the output?

10 10 Example: What is the output? #include /* Test some Booleans. */ int main() { float whatever = 0.003; if (whatever) { printf(“Is that true, Homer?\n”); } else { printf(“No, Marge.\n”); } return 0; } Behavior is “undefined.”

11 11 Boolean Expressions...are either strictly true or strictly false. A Boolean expression which evaluates to true has integer value 1; otherwise, 0.

12 12 Boolean Operators...are used in forming Boolean expressions....are also known as “logical” operators And ( && ) Or ( || ) Not ( ! ) Equality ( == ) Inequality ( != ) Comparison (, = )

13 13 And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 tall && dark && handsome Examples:

14 14 And True only if both Boolean arguments are true. 1 && 2 1 && 0 && -1 tall && dark && handsome Examples: not to be confused with “bitwise AND” ( & )

15 15 Or True only if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples:

16 16 Or True only if either Boolean argument is true (or both are true). 1 || 2 11 || 0 || 1 good || bad || ugly Examples: not to be confused with “bitwise OR” ( | )

17 17 Not True only if the single Boolean argument is false. ! 1 ! 0 ! happy Examples:

18 18 Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples:

19 19 Equality True only if both arguments have identical values. 1 == 2 1 == 0 42 == 42 truth == beauty Examples: not to be confused with assignment ( = )

20 20 Inequality True only if arguments have different values. 1 != 2 1 != 0 42 != 42 truth != beauty Examples:

21 21 Comparison True only if the specified relationship holds 1 < 2 0 > 1 42 <= 42 age >= 18 Examples:

22 22 Comparison True only if the specified relationship holds 1 < 2 0 > 1 42 <= 42 age >= 18 Careful! Examples:

23 23 Precedence Highest to lowest: –Brackets –Not ( ! ) –Comparison (, = ) –Equality ( == ) –Inequality ( != ) –And ( && ) –Or ( || ) Note: The assignment operator (=) is lower in order of precedence than Boolean / Logical operators.

24 24 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.c

25 25 Common Mistakes Using = instead of ==

26 26 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.c

27 27 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.c Usual error message (if any): “LValue required...”

28 28 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.c

29 29 Common Mistakes Using = instead of == Multiple comparisons

30 30 Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

31 31 #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1

32 32 #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score < 48 ) { printf("Fail\n"); } return 0; } Example: boolerr2.c 0 or 1 always 1

33 33 Example: #include /* Another common C error. */ int main() { int score; scanf("%d", &score); if ( 0 < score && score < 48 ) { printf("Fail\n"); } return 0; } boolerr2.c

34 34 Summary Boolean Type int as Boolean Boolean expressions Boolean Operators Precedence Common mistakes

35 35 Readings This Lecture: D&D 2/e: Sections 2.12 to 2.13, 3.2 and 4.13 to 4.15 D&D 3/e: Sections 2.6, 3.5, 4.10 and 4.11 Next Lecture: Selection D&D 2/e: Sections 3.2 to 3.3 D&D 3/e: Sections 3.4 to 3.6


Download ppt "1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver."

Similar presentations


Ads by Google