Just like in mathematics and statistics, Logical Operators are also needed in programming languages. Logical operators or Logical connective is a constant which is used to connect or add two or more formulas or variables. They are used to find out the Logic between values or variables.
While Relational Operators are used for testing whether a particular condition is true or false, they only test one single condition at a time. There comes a time when we need to test more than one condition at a single time. This situation is tackled by the Logical Operators. In computer science, Logical Operators provides us the functionality of testing multiple conditions at a single time.
Definition
- In C++, Logical Operators are used for testing and combining two or more conditions at a single time or complement the evaluation of the given condition.
- Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable.
Logical Operators
Use – In programming languages, Logical Operators are mainly used in conditional statements and loops for evaluating the conditions. Logical operators are used to check whether an expression is True or False.
After using Logical Operators on the variables, the result returned is a Boolean Value i.e., Either a True or False value. By default, the operand values are converted into type Boolean and then compared and computed.
Syntax
operand_1 operator operand_2 ;
where,
operand = expression / variable / value
operator = logical operator symbol
There are a lot many types of Logical Connectives in Mathematics. But the most commonly found Logical Connectives or Logical Operators include Conjunction, Disjunction, and Negation. In mathematical terminologies, Conjunction is denoted as ‘^’, Disjunction is denoted as ‘v’, and Negation is denoted as ‘~’. But in programming terminologies, we use different notations.
In a programming language, Logical Conjunction is called as Logical AND operation, Logical Disjunction is called as Logical OR operation and Logical Negation is called as Logical NOT operation.
These notations are as follows :
Operator | Name | Form |
&& | Logical AND | a && b |
|| | Logical OR | a || b |
! | Logical NOT | !a |
Note – Operands can contain Positive values as well as Negative Values. In C++, Negative values are considered as positive and are treated as True when they are used in any Conditions along with a Logical Operator.
Browse all the Topics Under Operator and Expressions: Operators
- Arithmetic Operators
- Assignment Operator
- C++ Shorthands
- Unary Operator
- Increment and Decrement Operators
- Relation Operator
Types of Logical Operators
In C, C++ programming languages, there are 3 logical operators. Let us understand each one of them in detail.
-
Logical AND Operator (&&)
The Logical AND Operator works on the following Truth Table of AND Logic:
Inputs | Output | |
A | B | A && B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
where, 0 = False, 1 = True in programming language
Logical AND Operator in a programming language is denoted by the symbol ‘&&’.
- The Logical AND Operator returns the Boolean value True or 1 when both the operands satisfy the conditions and are true in nature.
- If any one of the operands does not satisfy the condition, then it will return a False or 0 value.
The operand values are by default converted into Boolean values and then the result is computed. Hence, the result returned by the operator is of type Boolean. Logical AND has left-to-right associativity.
Note – Do not get confused between the ‘&&’ and ‘&’ operators. The ‘&’ symbol represents the Bitwise AND operator. This operator performs AND function on every Bit of the two values of the operands.
Example
int main()
{
int x = 10 ;
int y = 20 ;
cout << ((x == 0) && (x > y)) << endl ;Â Â Â Â Â Â Â Â // False && False = False
cout << ((x == 0) && (x < y)) << endl ;Â Â Â Â Â Â Â Â // False && True = False
cout << ((x == 10) && (x > y)) << endl ;Â Â Â Â Â Â // True && False = False
cout << ((x == 10) && (x < y)) << endl ;Â Â Â Â Â Â // True && True = True
return 0;
}
Output
0
0
0
1
-
Logical OR Operator (||)Â
The Logical OR Operator works on the following Truth Table of OR Logic:
Inputs | Output | |
A | B | A || B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
where, 0 = False, 1 = True in programming language
Logical OR Operator in the programming language is denoted by 2 vertical lines symbol ‘||’.
- The Logical OR Operator returns the Boolean value True or 1 when any one of the operands satisfies the conditions and are true in nature.
- If both the operands do not satisfy the condition, then it will return a False or 0 value.
Logical OR has left-to-right associativity.
Note – Do not get confused between the ‘||’ and ‘|’ operators. The ‘|’ symbol represents the Bitwise OR operator. This operator performs OR function on every Bit of the two values of the operands.
Example
int main()
{
int x = 5 ;
int y = 15 ;
cout << ((x == 0) || (x > y)) << endl ;Â Â Â Â Â Â Â Â Â Â // False || False = False
cout << ((x == 0) || (x < y)) << endl ;Â Â Â Â Â Â Â Â Â Â // False || True = True
cout << ((x == 5) || (x > y)) << endl ;Â Â Â Â Â Â Â Â Â Â // True || False = True
cout << ((x == 5) || (x < y)) << endl ;Â Â Â Â Â Â Â Â Â Â // True || True = True
return 0;
}
Output
0
1
1
1
-
Logical NOT Operator (!)Â
Logical NOT Operator is a Unary Operator i.e., it requires only one operand to work on. Following is the Truth Table of NOT Logic:
Input | Output |
A | !A |
0 | 1 |
where, 0 = False, 1 = True in programming language
Logical OR Operator in the programming language is denoted by the symbol ‘!’.
- The Logical NOT Operator returns a negate value i.e. if the condition is not satisfied then it will return a True value.
- If the condition is satisfied, then it returns a False value.
Example
int main()
{
int x = 10 ;
int y = 20 ;
cout << !(x<y) << endl ;Â Â Â Â Â Â // x < y is True, but NOT Operator will return False i.e. 0
cout << !(y == 0) << endl ;Â Â Â // y is not 0, but NOT operator will return True i.e. 1
return 0;
}
Output
0
1
Order of Precedence
Many a time, we think that Logical AND and Logical OR Operators have the same order of precedence. However, Logical AND has a higher preference than Logical OR operator. Thus, the Logical AND operator will be evaluated first and then the Logical OR operator will be evaluated if they exist in the same statement.
Order of Precedence :
Logical NOT > Logical AND > Logical OR
Example –
- value1 || value 2 && value3
This statement will first evaluate (value2 && value3) and then Logically OR it with value1.
Thus, when mixing Logical ANDs and Logical ORs, it is recommended to define each operator and its operand explicitly with proper parenthesis. Thus the above statement can be improved and written as,
value1 || (value2 && value3)
FAQs on Logical Operators
Q1. Boolean Operator used for Logical AND is?
- ||
- ^
- &&
- !=
Answer. Option C
Q2. What will be the output of the following code?
int main()
{
cout << ((7>2) && (1<2)) ;
cout << ((2>8) || (5<1)) ;
cout << !(1>4)) ;
return 0;
}
- 0 1 0
- 1 0 1
- 1 0 0
- 0 0 1
Answer. Option B
Solution.
Step 1: 7 > 2 = True/1
   1 < 2 = True/1
   True && True = True/1
Step 2: 2 > 8 = False/0
   5 < 1 = False/0
   False || False = False/0
Step 3: 1 > 4 = False/0
   !(0) = 1
Step 4: Final Answer = 1 0 1
Q3. What is the output of the following program?
int main()
{
int x = 9 ;
int y = 10 ;
cout << !(x == y) ;
return 0;
}
- 1
- 0
- Syntax Error
- None of these
Answer. Option A
Q4. In programming languages, Logical Operators returns what type of values?
- Integer Type
- Floating Type
- Character Type
- Boolean Type
Answer. Option D
Q5. The ‘|’ operator is a type of Logical Operator?
- True
- False
Answer. Option B
Leave a Reply