Operator Precedence in Relational Operators

Relational operators are used to compare two values. The precedence of relational operators from highest to lowest is as follows:

  • Less than, greater than, less than or equal to, greater than or equal to < > <= >=
  • Equality and inequality == !=

Below is the implementation of Operator Precedence in Relational Operators:

C++
#include <iostream>
using namespace std;

int main()
{
    int a = 15;
    int b = 20;
    int c = 35;

    // Using relational operators
    // b < c is evaluated first, which results to 1 and then
    // 1 != 35 is evaluated which results to true
    bool result1 = b < c != c;
    // c != c is evaluated first, which results to 0 and
    // then 20 < 0 is evaluated which results to false
    bool result2 = b < (c != c);

    // Print the results
    cout << "(b < c != c) = " << result1 << endl;
    cout << "(b < (c != c)) = " << result2 << endl;

    return 0;
}

Output
a < b: 1
b > c: 0
a <= c: 1
c >= b: 1
a == b: 0
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

Similar Reads

What is Operator Precedence?

Operator Precedence is a set of rules that defines the order in which operations are performed in an expression based on the operators between the operands. Consider the following mathematical expression 2 + 3 * 4. If we perform the operations from left to right, we get (2 + 3) * 4 = 20. However, if we follow the mathematical rule of precedence (also known as BODMAS), which states that multiplication and division should be performed before addition and subtraction, we get 2 + (3 * 4) = 14. This rule of precedence is also applicable in programming....

Operator Precedence in Arithmetic Operators

Arithmetic operators follow the standard precedence rules that are used in mathematics. The precedence of arithmetic operators from highest to lowest is as follows:...

Operator Precedence in Relational Operators

Relational operators are used to compare two values. The precedence of relational operators from highest to lowest is as follows:...

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:...

Operator Precedence in Assignment Operators

Assignment operators are used to assign values to variables. The assignment operator = has the lowest precedence....

Operator Precedence in Bitwise Operators

Bitwise operators operate on binary representations of integers. The precedence of bitwise operators from highest to lowest is as follows:...

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....

Operator Precedence in Unary Operators

Unary operators operate on a single operand. The precedence of unary operators is higher than arithmetic, relational, logical, and assignment operators....

Operator Precedence in Member Access Operators

Member access operators . and -> have the highest precedence....

Operator Precedence in Type Cast Operators

Type cast operators are used to convert one data type to another. They have higher precedence than arithmetic, relational, logical, and assignment operators....

Importance of Operator Precedence:

Understanding operator precedence is essential for several reasons:...