Operator Precedence in Conditional (Ternary) Operator
The conditional (ternary) operator ?: has lower precedence than arithmetic, relational, and logical operators but higher precedence than the assignment operator.
Below is the implementation of Operator Precedence in Conditional Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 55;
int b = 100;
// Using conditional (ternary) operator
string result = (a > b) ? "a is greater than b"
: "a is not greater than b";
// Print the result
cout << result << endl;
return 0;
}
Output
a is not greater than b
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