Lect 6
Lect 6
Booleans/Logical operators
Lecture 6
1
Topics
Boolean
Type int as Boolean
Boolean expressions
Boolean Operators
Precedence
Common mistakes
2
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
3
Boolean Algebra --- History
George Boole, 1815-1864
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
Example: What is the output?
#include <stdio.h>
int main()
{
int whatever = 0;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
6
Example: What is the output?
#include <stdio.h>
int main()
{
int whatever = 1;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
7
Example: What is the output?
#include <stdio.h>
int main()
{
int whatever = -100;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
8
Example: What is the output?
#include <stdio.h>
int main()
{
int whatever = ’A’;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
9
Example: What is the output?
#include <stdio.h>
int main()
{
int whatever = 0.003;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
10
Example: What is the output?
Behavior is
#include <stdio.h>
“undefined.”
/* Test some Booleans. */
int main()
{
float whatever = 0.003;
if (whatever)
{
printf(“Is that true, Homer?\n”);
}
else
{
printf(“No, Marge.\n”);
}
return 0;
}
11
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
12
Boolean Operators
...are used in forming Boolean expressions.
...are also known as “logical” operators
And (&&)
Or (||)
Not (!)
Equality (==)
Inequality (!=)
Comparison (<, >, <=, >=)
13
And
True only if both Boolean arguments are
true.
Examples:
1 && 2
1 && 0 && -1
14
And
True only if both Boolean arguments are
true.
Examples:
1 && 2
not to be
confused with
1 && 0 && -1
“bitwise
AND” (&)
healthy && wealthy && wise
15
Or
True if either Boolean argument is true (or
both are true).
Examples:
1 || 2
11 || 0 || 1
16
Or
True only if either Boolean argument is true
(or both are true).
not to be confused
with “bitwise OR” (|)
Examples:
1 || 2
11 || 0 || 1
17
Not
True only if the single Boolean argument is
false.
Examples:
! 1
! 0
! happy
18
Reminder: Gates
A B A AND B A OR B
0 0
0 1
1 0
1 1
19
Reminder: Gates
NOT Gate
A NOT A
0
1
20
Equality
True only if both arguments have identical
values.
Examples:
1 == 2
1 == 0
42 == 42
truth == beauty
21
Equality
True only if both arguments have identical
values.
Examples:
1 == 2
not to be
confused with
1 == 0
assignment
(=)
42 == 42
truth == beauty
22
Inequality
True only if arguments have different
values.
Examples:
1 != 2
1 != 0
42 != 42
truth != beauty
23
Comparison
True only if the specified relationship holds
Examples:
1 < 2
0 > 1
42 <= 42
age >= 18
24
Short-circuiting
A complex Boolean expression is only
evaluated as far as necessary
Examples:
1 || 2
0 || 1 || 2
1 && 0 && -1
25
Precedence
Highest to lowest:
Brackets
Not (!)
Comparison (<, >, <=, >=)
Equality (==)
Inequality (!=) Note: The assignment
operator (=) is lower in
And (&&) order of precedence
than Boolean / Logical
Or (||)
operators.
26
Example:
#include <stdio.h>
int main()
{
int age = 18;
int haveMoney = 0;
int haveCard = 1;
float thirst = 0.31;
int afterHours = 1;
int result;
printf("%d\n", result);
return 0;
} bool.c
27
Common Mistakes
Using = instead of ==
28
Example:
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
return 0;
}
boolerr1.c
29
Example:
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
return 0;
}
boolerr1.c
31
Common Mistakes
Using = instead of ==
Multiple comparisons
32
Example:
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
return 0;
}
boolerr2.c
33
Example:
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
#include <stdio.h>
int main()
{
int score;
always 1
scanf("%d", &score);
#include <stdio.h>
int main()
{
int score;
scanf("%d", &score);
return 0;
}
boolerr2.c
36
Conditional Expressions
A ternary operator
For example:
int z,a,b;
…
z = (a>b) ? a : b ;
37