After studying this chapter you will be able to understand: Learn about control structures; examine relational and logical operators; explore how to form and evaluate logical (boolean) expressions; discover how to use the selection control structures if, if...else, and switch in a program; learn to use the assert function to terminate a program.
Trang 1Lecture 26
Trang 2Summary of previous lecture
In the previous lecture, we have been learnt,
Trang 4 A special “type” with only two values:
false and true
Used to implement conditions
for selection and looping in an algorithm
Trang 5Boolean Algebra - History
George Boole, 1815-1864
Initially self-guided studies of languages and philosophy
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
Trang 6Type int as Boolean
In C, integers are used as Booleans
Integer value 0 is false
Any non-zero integer value is true
Trang 7#include <stdio.h>
/* 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?
Trang 8#include <stdio.h>
/* 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?
Trang 9#include <stdio.h>
/* 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?
Trang 10#include <stdio.h>
/* 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?
Trang 11#include <stdio.h>
/* 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?
Trang 13Boolean Expressions
are either strictly true or strictly false
can be evaluated to determine their true
Trang 210 1
NOT A
NOT Gate
Reminder: Gates
Trang 22Examples:
Trang 24Examples:
Trang 25Examples:
Trang 27operators
Trang 28Boolean Algebra
NOT false = true
NOT true = false
true AND true = true
true AND false = false
true OR true = true
true OR false = true
false AND false = false
false OR false = false
if (0 || 0) printf("Does false OR false = true?");
if (0 && 0) printf("Does false AND false = true?");
Trang 29result = age >= 18 && (haveMoney || haveCard)
&& thirst > 0.3 && ! afterHours;
printf("%d\n", result);
return 0;
Trang 30Common Mistakes
Using = instead of ==
Trang 34Common Mistakes
Using = instead of ==
Multiple comparisons
Trang 39True 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
Trang 40Exclusive 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?
Trang 41Two out of three…
True if and only if exactly two of A,B,C are true
Define!
Is there a better way?
Trang 42Simplifying & Checking Boolean
Expressions
Use Truth Tables
Transform Expressions using Boolean Algebra
Trang 43Axioms of Boolean Algebra
Trang 44Axioms of Boolean Algebra (cont)
Trang 47More Examples
ch1 = 'a';
ch2 = 'a';
printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1
ch1 = 'd';
ch2 = 'f';
printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1
ch1 = 'a';
ch2 = '\0';
printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0