![Page 1: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/1.jpg)
1
Revision of IO
• Streams
• printf
• scanf
![Page 2: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/2.jpg)
2
CSE1301 Computer Programming
Lecture 8Booleans
![Page 3: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/3.jpg)
3
Topics
• Boolean
• Type int as Boolean
• Boolean expressions
• Boolean Operators
• Precedence
• Common mistakes
![Page 4: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/4.jpg)
4
Boolean
• A special “type” with only two values: – false and true.
• Used to implement conditions – for selection and looping in an algorithm
• Boolean expressions – represent statements which are either strictly
true or strictly false
![Page 5: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/5.jpg)
5
Boolean Algebra --- History
• George Boole, 1815-1864
– Initially self-guided studies of languages and philosphy– With 16 assistant teacher at private school – With 20 opened a school and taught himself mathematics– Published in “Cambridge Mathematical Journal” with 24
• The Mathematical Analysis of Logic was published in 1847– Casts logical reasoning in the form of algebra
• + is OR, * is AND• 0 is FALSE, 1 is TRUE
• An Investigation of the Laws of Thought (1854) extends the algebra
![Page 6: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/6.jpg)
6
Type int as Boolean
• In C, integers are used as Booleans
• Integer value 0 is false.
• Any non-zero integer value is true.
![Page 7: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/7.jpg)
7
#include <stdio.h>
/* 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?
![Page 8: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/8.jpg)
8
#include <stdio.h>
/* 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?
![Page 9: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/9.jpg)
9
#include <stdio.h>
/* 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?
![Page 10: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/10.jpg)
10
#include <stdio.h>
/* 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?
![Page 11: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/11.jpg)
11
#include <stdio.h>
/* 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?
![Page 12: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/12.jpg)
12
Example: What is the output?
#include <stdio.h>
/* 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.”
![Page 13: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/13.jpg)
13
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 true
![Page 14: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/14.jpg)
14
Boolean Operators
• ...are used in forming Boolean expressions.• ...are also known as “logical” operators• And (&&)• Or (||)• Not (!)• Equality (==)• Inequality (!=)• Comparison (<, >, <=, >=)
![Page 15: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/15.jpg)
15
And
• True only if both Boolean arguments are true.
1 && 2
1 && 0 && -1
healthy && wealthy && wise
Examples:
![Page 16: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/16.jpg)
16
And
• True only if both Boolean arguments are true.
1 && 2
1 && 0 && -1
healthy && wealthy && wise
Examples:
not to be confused with
“bitwise AND” (&)
![Page 17: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/17.jpg)
17
Or
• True if either Boolean argument is true (or both are true).
1 || 2
11 || 0 || 1
good || bad || ugly
Examples:
![Page 18: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/18.jpg)
18
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” (|)
![Page 19: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/19.jpg)
19
Not
• True only if the single Boolean argument is false.
! 1
! 0
! happy
Examples:
![Page 20: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/20.jpg)
20
Reminder: Gates
A B
0 0
0 1
1 0
1 1
AND Gate
A AND B A OR B
OR Gate
![Page 21: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/21.jpg)
21
A
0
1
NOT A
NOT Gate
Reminder: Gates
![Page 22: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/22.jpg)
22
Equality
• True only if both arguments have identical values.
1 == 2
1 == 0
42 == 42
truth == beauty
Examples:
![Page 23: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/23.jpg)
23
Equality
• True only if both arguments have identical values.
1 == 2
1 == 0
42 == 42
truth == beauty
Examples:
not to be confused with
assignment (=)
![Page 24: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/24.jpg)
24
Inequality
• True only if arguments have different values.
1 != 2
1 != 0
42 != 42
truth != beauty
Examples:
![Page 25: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/25.jpg)
25
Comparison
• True only if the specified relationship holds
1 < 2
0 > 1
42 <= 42
age >= 18
Examples:
![Page 26: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/26.jpg)
26
Short-circuiting
• A complex Boolean expression is only evaluated as far as necessary
1 || 2
0 || 1 || 2
1 && 0 && -1
Examples:
![Page 27: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/27.jpg)
27
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.
![Page 28: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/28.jpg)
28
Example:#include <stdio.h>
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
![Page 29: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/29.jpg)
29
Common Mistakes
• Using = instead of ==
![Page 30: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/30.jpg)
30
Example:
#include <stdio.h>
/* 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
![Page 31: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/31.jpg)
31
Example:
#include <stdio.h>
/* 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...”
![Page 32: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/32.jpg)
32
Example:
#include <stdio.h>
/* 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
![Page 33: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/33.jpg)
33
Common Mistakes
• Using = instead of ==• Multiple comparisons
![Page 34: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/34.jpg)
34
Example:
#include <stdio.h>
/* Another common C error. */
int main(){ int score;
scanf("%d", &score);
if ( 0 < score < 48 ) { printf("Fail\n"); }
return 0;}
boolerr2.c
![Page 35: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/35.jpg)
35
#include <stdio.h>
/* 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
![Page 36: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/36.jpg)
36
#include <stdio.h>
/* 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
![Page 37: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/37.jpg)
37
Example:
#include <stdio.h>
/* Another common C error. */
int main(){ int score;
scanf("%d", &score);
if ( 0 < score && score < 48 ) { printf("Fail\n"); }
return 0;}
boolerr2.c
![Page 38: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/38.jpg)
38
True and False as Constants
• ‘C’ does not provide constants for TRUE/FALSE (other than 0 and 1)
• You can make use of the pre-processor
• Use– #define TRUE 1– #define FALSE 0
whenever you need such constants
![Page 39: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/39.jpg)
39
Exclusive OR
• True if “at most one” of two alternatives is true• False if neither is true• False if both are true!
• Define!
A xor B :
Do you need brackets?
![Page 40: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/40.jpg)
40
Two out of three…
• True if and only if exactly two of A,B,C are true
• Define!
• Is there a better way?
![Page 41: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/41.jpg)
41
Simplifying & Checking Boolean Expressions
• Use Truth Tables
• Transform Expressions using Boolean Algebra
0 0 0
1 0 1
0 1 1
1 1 0
A B A xor B
0 0
1 0
0 1
1 1
A B
A && !B || B && !A
![Page 42: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/42.jpg)
42
Axioms of Boolean Algebra
• Commutative– (A && B) (B && A)
– (A || B) (B || A)
• Associative– A && (B && C) (A && B) && C
– A || (B || C) (A || B) || C
![Page 43: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/43.jpg)
43
Axioms of Boolean Algebra (cont)
• Distributive
– A && (B || C) (A && B) || (A && C)
– A || (B && C) (A || B) && (A || C)
• … plus some “technicalities”
![Page 44: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/44.jpg)
44
De Morgan’s Law
• From the axioms of Boolean algebra it follows that:
– ! ( A || B ) !A && !B
– ! (A && B) !A || !B
• You can use all rules above to simplify / verify expressions.
• Exercise: simplify ! ( A || !B ) && A
![Page 45: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/45.jpg)
45
Conditional Expressions
• We can write an expression such that its value depends on a TRUTH value
Condition ? Expr2 : Expr3
– A ternary operator
– For example:
int z,a,b;…z = (a>b) ? a : b ;
![Page 46: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/46.jpg)
46
Summary
• Boolean
• Type int as Boolean
• Boolean expressions
• Boolean Operators
• Precedence
• Common mistakes
• Some Boolean algebra
![Page 47: 1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans](https://reader036.vdocuments.us/reader036/viewer/2022062300/56649d765503460f94a5823b/html5/thumbnails/47.jpg)
47
Readings
This Lecture:• King: Section 5.1• D&D: Sections 2.6, 3.4 – 3.6, 4.10• Kernighan & Ritchie: 2.6, 2.12