Broad Network


Boolean Logic and C++ Conditions

C++ Taking the Bull by the Horns – Part 8

Forward: In this part of the series we apply Boolean logic to C++ conditions.

By: Chrysanthus Date Published: 21 Aug 2012

Introduction

This is part 8 of my series, C++ Taking the Bull by the Horns. In this part of the series we apply Boolean logic to C++ conditions. For this part of the series, we assume that a tall man has a height of 20dm and a short man has a height of 10dm. I have exaggerated the numbers for emphasis.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Single Expression Example
Consider the following code:

#include <iostream>
using namespace std;

int main()
    {
        //tall means 20
        int me = 20;

        if (me == 20)
            {
                cout << "I am tall";
            }

        return 0;
    }

Read and try the code. In the condition, (parentheses of if) there is only one expression, which is, (me == 20). If this expression results in true, the if-block will be executed. In C++ the number 1 means true in a condition and the number 0 means false. In other words, 1 is the _Bool value for true and 0 is the _Bool value for false. The above if-construct is equivalent to

    if (1)
        {
            cout << "I am tall";
        }

For this second if-construct to be executed, you do not need the creation of the identifier and its assignment. Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
         if (1)
            {
                cout << "I am tall";
            }

        return 0;
    }

Let us look at a case where the condition results in false. Consider the following code:

#include <iostream>
using namespace std;

int main()
    {
        //short means 10
        int me = 10;

        if (me == 20)
            {
                cout << "I am tall";
            }

        return 0;
    }

The if-block (curly braces) in the above code will not be executed, because the condition results in false, since the value of the identifier, me, is 10 for “short” and not 20 for “tall”. The above if-construct is equivalent to:

if (0)
    {
        cout << "I am tall";
    }

An if-block can only be executed if the condition is true. In this last case it is not executed, since zero means false.

More than One Expression in Condition
You can have more than one expression in a condition. In this part of the series, I consider a maximum of two expressions in a condition. Each of the expressions results in true or false. The expressions are combined with the AND, OR or NOT operators. The AND operator is typed as, &&. The OR operator is typed as, || . The NOT Operator is typed as ! . &&, || , and ! are called logical operators. With logical operators, the rules in the previous part of the series can be rewritten as:

AND
(false) && (false) = false
(false) && (true) = false
(true) && (false) = false
(true) && (true) = true

OR
(false) || (false) = false
(false) || (true) = true
(true) || (false) = true
(true) || (true) = true

NOT
!(false) = true
!(true) = false

Double-Expression Examples
The if-block will not be executed in the following code:

#include <iostream>
using namespace std;

int main()
    {
        if ((0)&&(1))
            {
                cout << "We are tall";
            }

        return 0;
    }

A practical example for the above code is:

#include <iostream>
using namespace std;

int main()
    {
        //Tall means 20 and short means 10
        int you = 20;
        int me = 20;

        if ((you == 10)&&(me == 20))
            {
                cout << "We are tall";
            }

        return 0;
    }

20 is assigned to the identifier, you, and also to the identifier, me. The first expression in the condition results in false and the second one results in true. (false)&&(true) gives false as the effective Boolean value for the condition. So the block is not executed.

The if-block will be executed in the following code:

#include <iostream>
using namespace std;

int main()
    {
        if ((0)||(1))
            {
                cout << "We are tall";
            }

        return 0;
    }

A practical example for the above code is:

#include <iostream>
using namespace std;

int main()
    {
        //Tall means 20 and short means 10
        int you = 20;
        int me = 20;

        if ((you == 10)||(me == 20))
            {
                cout << "We are tall";
            }

        return 0;
    }

Read the above code. Try it. The first expression results in false; the second one results in true. The effective condition is true, since (false)||(true) gives true.

NOT Examples
The if-block will be executed in the following code:

#include <iostream>
using namespace std;

int main()
    {
        if (!(0))
            {
                cout << "I am tall";
            }

        return 0;
    }

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

#include <iostream>
using namespace std;

int main()
    {
        //Tall means 20 and short means 10
        int me = 20;

        if (!(me == 10))
            {
                cout << "I am tall";
            }

        return 0;
    }

else if and else
You can still add the else-if and else sub statements to the above code samples, following what we learned in one of the previous parts of the series.

A tall man in your area may actually have the height of 1.8m and not 2m as given above. Also a short man in your area may actually have a height of 1.4m and not 1m as given above. You may now ask the question, why did I not use 1.8 for tall and 1.4 for short and then use the float type instead of the int type for the above objects (identifiers). C++ does not handle the float object type in a straightforward way (see later).

Let us stop here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message