Broad Network


Logical Operators in C++

C++ Operators – Part 4

Forward: In this part of the series, we look at logical operators. I use the if-condition to explain logical operators.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 4 of my series, C++ Operators. Logical operators are the operators for the Boolean AND and Boolean OR. In this part of the series, we look at logical operators. I use the if-condition to explain logical operators.

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 Condition 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";
        }

Here 1 is true. 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 || , (and sometimes !) are called logical operators. With logical operators, the rules (truth tables) for AND, OR and NOT can be written 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()
    {
        //Let tall means 20 and short means 10
        int me = 20;

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

        return 0;
    }


Note: C++ does not handle the float object type in a straightforward way (see some other document for this).

Logical operators are && and ||. You can also consider ! as a logical operator. && and || are left-to-right operators, while ! is a right-to-left operator.

Time to take a break. We 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