Activity 7-1 - Using Boolean Expressions



The order in which the statements in a program are performed is called flow of control.  In the previous labs, you saw programs in which if, if...else, while, and do ... while statements were used.  These statements were used to specify flow of control in your programs.  The concept behind all these methods is the same, your program comes to a statement that is either true or false.  Such a statement is called a Boolean expression.  The program then will perform a different task depending on the statement being true or false.  For example: Suppose somewhere in your program you have:

if( x < 10 && y > 12).

In this statement, the Boolean expression is x < 10 && y > 12.  This expression is true or false.  Of course, there are several possibilities here:

    Possibility 1: x >= 10 and y <= 12            the expression will be: false
    Possibility 2: x >= 10 and y > 12              the expression will be: false
    Possibility 3: x < 10 and y <= 12              the expression will be: false
    Possibility 4: x < 10 and y > 12                the expression will be: true

So your program will go one way when the expression evaluates to false and another way when the expression evaluates to true.  So its flow is controlled by this Boolean expression.  Note that the expression inside (  ) in the if statement can be very simple or very complicated.  But, in either case that statement will evaluate to true or false, regardless of its complexity.  When an expression is complex, your compiler uses the precedence rules to decide the order in which each different part of the expression should be executed.  In the following expression, for example, the order of execution is decided by these rules:

if ( (x + 2) > 3 || (x + 1) < -3 )

will be executed in this order:

if (  ((x + 2) > 3) || ((x + 1) < -3 )

The rules are listed in Display 7.2 of the textbook.  Here, first the expression ((x + 2) > 3) will be evaluated and then the expression ((x + 1) < -3).  Because there is an OR, "| |", between the two expressions, if the first one evaluates to true, the second one never gets evaluated.  This is because, in an OR expression, once either one of the two expressions is true, the entire expression evaluates to true.

Exercise 7.1
Following is a list of some Boolean expressions.  Carefully go through each one of them and determine whether they evaluate to true or false.

                  Expression                                                Answer
    A:  ( (count == 0) && (limit < 20))
    B:  ( count == 0 && limit < 20 )
    C:  ( (limit > 12) || (count < 5) )
    D:  ( !(count == 5) )
    E:  ( (count == 1) && (x < y) )
    F:  ( (count < 10) || (x < y) )
    G:  ( !( ((count < 10) || (x < y)) && (count >= 0)) )
    H:  ( ((limit/count) > 7) || (limit < 20) )
    I:   ( (limit < 20) || ((limit/count) > 7) )
    J:   ( ((limit/count) > 7) && (limit < 0) )
    K: ( (limit < 0) && (( limit/count) > 7) )
    L: ( (5 && 7) + (!6) )

Now, create a file ex71.cpp and cut and paste the following program in it.  Compile and run the program and see how well your answers to the above expressions match the program's output.

// ex71.cpp - This program illustrates the Boolean expressions and the
// order of precedence
#include<iostream>
using namespace std;

int main()
{

    int count = 0, limit = 10;
    int x,y;

    cout << "a  " << ( (count == 0) && (limit < 20)) << "\n";
    cout << "b  " << ( count == 0 && limit < 20 ) << "\n";
    cout << "c  " << ( (limit > 12) || (count < 5) ) << "\n";
    cout << "d  " << ( !(count == 5) ) << "\n";
    cout << "e  " << ( (count == 1) && (x < y) ) << "\n";
    cout << "f  " << ( (count < 10) || (x < y) ) << "\n";
    cout << "g  " << ( !( ((count < 10) || (x < y)) && (count >= 0)) ) << "\n";
    cout << "h  " << ( ((limit/count) > 7) || (limit < 20) ) << "\n";
    cout << "i  " << ( (limit < 20) || ((limit/count) > 7) ) << "\n";
    cout << "j  " << ( ((limit/count) > 7) && (limit < 0) ) << "\n";
    cout << "k  " << ( (limit < 0) && (( limit/count) > 7) ) << "\n";
    cout << "l  " << ( (5 && 7) + (!6) ) << "\n";

    return 0;
}

Note that some of the statements may cause run-time errors.  Find those statements and explain why the errors have happened. To make the program go to the next statement, simply comment (//) the line that causes the run-time error. Recompile and run the program again until all lines that cause a run-time error are commented.

What is the difference between h and i?
 
 

Would it make any difference if in "j" we switch the first and the second statements, i.e., we use:
    cout << "j  " << ( (limit < 0) && ((limit/count) > 7)  ) << "\n";