The C++ needs logical operators to control the flow of the program. The expressions involving logical operators evaluate to boolean value – true or false. Then the program makes decisions based on the outcome.
There are three types of logical operators.
Logical Operators | Description |
&& | Logical AND |
|| | Logical OR |
! | Not |
Note that there is difference between bitwise AND and logical AND. The same applied to bitwise OR, complement.
Logical AND
The two operands of logical expression evaluates to true if both the operands are true, otherwise it is false.
A logical expression with two or more logical expression as operands is called a compound expression. When the two operands are individual expressions then each of them must evaluate to true so that the compound logical expression is true.
(expression1) && (expression2)
The data type for both the expression must be same. If expression1 is integer, then expression must be an integer. In case, one of the expression is character data type, then it is automatically converted to integer equivalent by the compiler.
(a && 34)
becomes
(97 && 34)
The ASCII value for ‘a’ is 97.
The all possible outcome of logical AND operation are:
Expression1 | Expression2 | Output |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Logical OR
The logical expression with logical OR operator evaluates to true if at least one operand is true. Otherwise, it is false.
A compound logical expression with two or more expression gives a boolean output – true if one of the expressions evaluates to true. The data type of each operand must be same except char type.
(expression1) && (expression2)
All possible outcome of the expression is given below.
Expression1 | Expression2 | Output |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Not Operator
The Not
operation is a special operation which negates the boolean value of any expression or variable.
expression1 = true
then
!(expression1) = false
The Not
operator can be use anywhere to negate the existing value of a variable or expression. The table below gives all combination of output for the Not
operator.
Expression1 | Description |
true | false |
false | true |
Example Program: Logical Operators
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//Variable Declarations
int a,b,c,d;
//Variable Initialization
a = 100;
b = 90;
c = 30;
d = 20;
// Logical AND
if( (a > b) && (c > d))
{
cout << "This logical AND statement has value = True" << "\n";
}
else
{
cout << "This logical AND statement has value = False" << "\n";
}
// Logical OR
if( (a < b) || (c > d))
{
cout << "This logical OR statement has value = True" << "\n";
}
else
{
cout << "This logical OR statement has value = False" << "\n";
}
// NOT operation
if( !(a > b))
{
cout << "This NOT statement has value = True" << "\n";
}
else
{
cout << "This NOT statement has value = False" << "\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}
Output:
This logical AND statement has value = True
This logical OR statement has value = True
This NOT statement has value = False