Operator Precedence in Logical Operators
Logical operators are used to combine two or more conditions. The precedence of logical operators from highest to lowest is as follows:
- Logical NOT !
- Logical AND &&
- Logical OR ||
Below is the implementation of Operator Precedence in Logical Operators:
#include <iostream>
using namespace std;
int main()
{
// Using logical operators
// true && false is evaluated first which results to
// false and then true || false is evaluated which
// results to true
bool result1 = true || false && false;
// true || false is evaluated first which results to
// true and then true && false is evaluated which
// results to false
bool result2 = (true || false) && false;
cout << "true || false && false = " << result1 << "\n";
cout << "(true || false) && false = " << result2
<< "\n";
return 0;
}
Output
a && b: 0 a || b: 1 !a: 0 a && (b || c): 1
Operator Precedence in Programming
Operator Precedence, also known as operator hierarchy, is a set of rules that controls the order in which operations are performed in an expression without parentheses. It is a fundamental concept in programming languages and is crucial for writing correct and efficient code.
Table of Content
- What is Operator Precedence?
- Operator Precedence in Arithmetic Operators
- Operator Precedence in Relational Operators
- Operator Precedence in Logical Operators
- Operator Precedence in Assignment Operators
- Operator Precedence in Bitwise Operators
- Operator Precedence in Conditional (Ternary) Operator
- Operator Precedence in Unary Operators
- Operator Precedence in Member Access Operators
- Operator Precedence in Type Cast Operators
- Importance of Operator Precedence
Contact Us