Week 3

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

4

the program should output

30

and if the user enters

13

the 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+1 is true, so A is printed.
  • In line 16, i < j and i < j+1 are both false, so i < j || i < j+1 is false. Therefore the entire condition is false, so D is printed.
  • In line 23, i-- evaluates to i, and then i is decremented, so E is printed since the original value of i was 1 (which has a Boolean value of true).
  • In line 26, ++i increments i, and then evaluates to i, so F is printed since the new value of i is 1 (which has a Boolean value of true).
  • In line 30, i = 0 assigns the value 0 to i and evaluates to 0 (which has a Boolean value of false). Then, i = 1 assigns the value 1 to i and evaluates to 1 (which has a Boolean value of true). Therefore the entire condition is true, so G is printed.