Logical operators

In addition to the assignment operator , = , the logical operators are given below.
Operator
Symbol
Form
Operation
Logical NOT
!
!x
true if x is false, or false if x is true
Logical AND
&&
x && y
true if both x and y are true, false otherwise
Logical OR
||
x || y
true if either x or y are true, false otherwise
The operators all give a result of type bool and a value of true or false. The logical negation operator, ! , is a unary operator turning true into false, and false into true. Let’s take a look into the next example.
#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       bool b1 = true, b2 = false;
       bool b3 = !b1;
       bool b4 = !b2;
       cout << b3 << "\n" << b4 << "\n";
       system("pause");
       return 0;
}
And with all unary operators, the logical negation operator associates right to left. The logical AND operator, && , is a binary operator. If both operands are true then the result is true, otherwise the result is false. The following code demonstrates using && operators:

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       bool b1 = true, b2 = false;
       cout << "If the statement is true the compiler will print 1. " << "\n";
       cout << "If the statement is false the compiler will print 0." << "\n";
       bool b3 = !b1;
       cout << "The result of the statement b3 = !b1 is: " << b3 << "\n";
       bool b4 = !b2;
       cout << "The result of the statement b4 = !b2 is: " << b4 << "\n";
       bool b5 = b1 && b2;
       cout << "The result of the statement b5 = b1 && b2 is: " << b5 << "\n";
       bool b6 = b2 && b3;
       cout << "The result of the statement b6 = b2 && b3 is: " << b6 << "\n";
       bool b7 = b1 && b3;
       cout << "The result of the statement b7 = b1 && b3 is: " << b7 << "\n";
       bool b8 = b1 && b4;
       cout << "The result of the statemnet b8 = b1 && b4 is: " << b8 << "\n";
       bool b9 = b2 && b4;
       cout << "The result of the statement b9 = b2 && b4 is: " << b9 << "\n";
       bool b10 = b3 && b4;
       cout << "The result of the statemnet b10 = b3 && b4 is: " << b10 << "\n";
       system("pause");
       return 0;
}
The result is shown in next figure.

As you can see from the previous figure the false or 0 is assigned to b5, b6, b7, b9 and b10.
The logical OR operator, || , is also binary operator. If either operand is true , than the result is true, otherwise the result is false. The following code will give a better demonstration of || operator.
// Logicaloperators003.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       bool b1 = true, b2 = false;
       cout << "If the statement is true the compiler will print 1. " << "\n";
       cout << "If the statement is false the compiler will print 0." << "\n";
       bool b3 = !b1;
       cout << "The result of the statement b3 = !b1 is: " << b3 << "\n";
       bool b4 = !b2;
       cout << "The result of the statement b4 = !b2 is: " << b4 << "\n";
       bool b5 = b1 || b2;
       cout << "The result of the statement b5 = b1 || b2 is: " << b5 << "\n";
       bool b6 = b2 || b3;
       cout << "The result of the statement b6 = b2 || b3 is: " << b6 << "\n";
       bool b7 = b1 || b3;
       cout << "The result of the statement b7 = b1 || b3 is: " << b7 << "\n";
       bool b8 = b1 || b4;
       cout << "The result of the statemnet b8 = b1 || b4 is: " << b8 << "\n";
       bool b9 = b2 || b4;
       cout << "The result of the statement b9 = b2 || b4 is: " << b9 << "\n";
       bool b10 = b3 || b4;
       cout << "The result of the statemnet b10 = b3 || b4 is: " << b10 << "\n";
       system("pause");
       return 0;
}
The result is shown in next figure.

The output of this program shows that true is assigned to b5, b7, b8, b9 and b10. 
Share on Google Plus