Boolean Expression

  1. A boolean expression is an expression that has relational and/or logical operators operating on boolean variables. A boolean expression evaluates to either true or false.

  2. Relational operators are:

       ==    is identical to
       !=    is not identical to
       <      is less than
       <=   is less than or equal to
       >     is greater than
       >=   is greater than or equal to

    Relational operators can operate on integers, floats, doubles and even strings (in a lexicographical fashion).

  3. Logical operators are:

       !       logical negation
       &&   logical AND
       ||   logical OR

    Logical operators operate on boolean variables or boolean expressions only.

  4. Examples of Boolean expression:

    long int winning_lottery_number = 947423945645, your_ticket_number;
    bool is_a_winner;
    cout << "Enter your lottery ticket number: ";
    cin >> your_ticket_number;
    is_a_winner = (your_ticket_number == winning_ticket_number); // can omit the parentheses
    cout << "Please tell me if I won! ";
    cout << "Output a 1 if I won, otherwise output a 0: ";
    cout << is_a_winner << endl;

    Another example:

    bool is_married, is_with_children, should_shoot_myself;
    cout << "Are you married?\n";
    cout << "Enter 1 if you are, otherwise enter 0: ";
    cin >> is_married;
    cout << "Do you have any children?\n";
    cout << "Enter 1 if you do, otherwise enter 0: ";
    cin >> is_with_children;
    should_shoot_myself = (is_married && is_with_children); // can omit the parentheses
    cout << "Output a 1 if I should shoot myself, otherwise output a 0: ";
    cout << should_shoot_myself;

    This example assumes that exam_score and number_of_missing_HW are integers:

    bool is_failing;
    is_failing = (exam_score < 55) || (number_of_missing_HW >= 3);
    // both pairs of parentheses are there for readability reasons only

  5. Most C++ compilers will treat any nonzero number as if it were the value true and will treat zero as if it were the value false.

  6. Careless use of Boolean expressions can lead to unexpected results.

  7. For example, a student fails a course if his score is less than the class average, otherwise the student passes the course. Someone may write the following code:

    if ( ! score > average )
        cout < < "You failed the course.\n";
    else
        cout < < "You passed the course.\n";

    The person read the code as saying: if it is not true that the score is higher than the average, then you failed the course, otherwise you passed the course.

    The above code will not cause problem during compilation, and it will also run without run-time error. However the code actually has a mistake.

    Suppose the score is 34 and the average is 63. The compiler applies the precedence rule and interprets the Boolean expression as the following

    ( ! score ) > average

    This turns out to evaluates to false for the following reasons. First because ! is a unary logical negation operator and the value of score is nonzero and so score is converted to true. Thus !score evaluates to false. The next operator > compares 2 numbers and so !score is converted to 0, and then this value is compared with 63. Since 0 is not greater than 63, the entire expression evaluates to false, and the output will be: You passed the course.

    Of course the correct way is to use parentheses and write the above Boolean expression as follows:

    if ( ! (score > average) )
        cout < < "You failed the course.\n";
    else
        cout < < "You passed the course.\n";

  8. C++ uses short-circuit evaluation of compound Boolean expressions for the sake of efficiency.

    • An expression such as (a && b) is false if either a or b is false. The program first evaluates a to see if it is true or false. If a is found to be false, the program will set (a && b) as false without evaluate b at all.

    • Similarly an expression such as (a || b) is true if either a or b is true. The program first evaluates a to see if it is true or false. If a is found to be true, the program will set (a || b) as true without evaluate b at all.

  9. The following example shows how this C++ feature can be exploited to avoid run-time error that will occur if count happens to be zero:

    if (count != 0) && ((sum_of_scores/double(count)) < 35)
        cout < < "What a low class average. Fire the professor!\n";

    Make sure you revisit this example after we cover the if statement.

  10. The NOT operator is a unary operator. It negates a boolean variable or expression, turning them from true to false and vice versa.

    Note that if a, b, c and d are integers, for example, then

    Expression Equivalent Expression
    !(a > b) a <= b
    !(a == b) a != b
    !(a == b || c == d) a != b && c != d
    !(a == b && c > d) a != b || c <= d