Boolean operators
AND/conjunction
| T | F | |
|---|---|---|
| T | T | F |
| F | F | F |
- C++ syntax:
&& - Mathematical notation: $\land$
OR/disjunction
| T | F | |
|---|---|---|
| T | T | T |
| F | T | F |
- C++ syntax:
|| - Mathematical notation: $\lor$
NOT/negation
| T | F |
| F | T |
- C++ syntax:
! - Mathematical notation: $\neg$
Boolean algebra
Distributive laws
$$ \begin{align} a \land (b \lor c) &= (a \land b) \lor (a \land c) \\ a \lor (b \land c) &= (a \lor b) \land (a \lor c) \end{align} $$
De Morgan’s laws
$$ \begin{align} \neg(a \land b) &= (\neg a) \lor (\neg b) \\ \neg(a \lor b) &= (\neg a) \land (\neg b) \end{align} $$
Conditionals (if/else)
Exercise 3.1
Write a program that prompts the user for a month number and outputs the number of days in the corresponding month. If the user enters an invalid month number (i.e., outside the range 1–12), the program should print an error message. You may assume that the year is not a leap year (so that month 2 has 28 days).
For example, if the user enters
4the program should output
30and if the user enters
13the program should output
Invalid month number!
Solution
Here is one possible solution, using
if/else.#include <iostream> using namespace std; int main() { int month; cout << "Enter a month number: "; cin >> month; if (month < 1 || month > 12) { cout << "Invalid month number!"; } else { if (month == 2) { cout << 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { cout << 30; } else { cout << 31; } } return 0; }Alternative solution
Here is another possible solution, using
switch.#include <iostream> using namespace std; int main() { int month; cout << "Enter a month number: "; cin >> month; if (month < 1 || month > 12) { cout << "Invalid month number!"; } else { switch (month) { case 2: cout << 28; break; case 4: case 6: case 9: case 11: cout << 30; break; default: cout << 31; } } return 0; }
Exercise 3.2
What is the output of the following program?
1#include <iostream> 2 3using namespace std; 4 5int main() { 6 int i = 1; 7 int j = -1; 8 9 if (i > j+1) { 10 cout << 'A'; 11 } 12 else if (i > j) { 13 cout << 'B'; 14 } 15 16 if ((i < j || i < j+1) && (i > j || i > j+1)) { 17 cout << 'C'; 18 } 19 else { 20 cout << 'D'; 21 } 22 23 if (i--) { 24 cout << 'E'; 25 } 26 if (++i) { 27 cout << 'F'; 28 } 29 30 if ((i = 0) || (i = 1)) { 31 cout << 'G'; 32 } 33 else { 34 cout << 'H'; 35 } 36 37 return 0; 38}Solution
The output is:
ADEFG
- In line 9,
i > j+1istrue, soAis printed.- In line 16,
i < jandi < j+1are bothfalse, soi < j || i < j+1isfalse. Therefore the entire condition isfalse, soDis printed.- In line 23,
i--evaluates toi, and theniis decremented, soEis printed since the original value ofiwas1(which has a Boolean value oftrue).- In line 26,
++iincrementsi, and then evaluates toi, soFis printed since the new value ofiis1(which has a Boolean value oftrue).- In line 30,
i = 0assigns the value0toiand evaluates to0(which has a Boolean value offalse). Then,i = 1assigns the value1toiand evaluates to1(which has a Boolean value oftrue). Therefore the entire condition istrue, soGis printed.