Operators are used to determine whether a specific relationship exists between two values

Skip to content

Kenneth Leroy Busbee

Overview

A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality [e.g., 5 = 5] and inequalities [e.g., 4 ≥ 3].[1]

Discussion

The relational operators are often used to create a test expression that controls program flow. This type of expression is also known as a Boolean expression because they create a Boolean answer or value when evaluated. There are six common relational operators that give a Boolean value by comparing [showing the relationship] between two operands. If the operands are of different data types, implicit promotion occurs to convert the operands to the same data type.

Operator symbols and/or names can vary with different programming languages. Most programming languages use relational operators similar to the following:

Operator Meaning
greater than
= greater than or equal to
== equality [equal to]
!= or inequality [not equal to]

Examples:

  • 9 < 25
  • 9 < 3
  • 9 > 14
  • 9 = 25
  • 9 == 13
  • 9 != 13
  • 9 !< 25
  • 9 25

Note: Be careful. In math you are familiar with using the symbol = to mean equal and ≠ to mean not equal. In many programming languages the ≠ is not used and the = symbol means assignment.

Key Terms

relational operatorAn operator that gives a Boolean value by evaluating the relationship between two operands.

References

  • cnx.org: Programming Fundamentals – A Modular Structured Approach using C++

Relational Operators

Relational operators are important for making decisions. They allow us compare numeric and char [chars are treated like numbers in C++] values to determine if one is greater than, less than, equal to, or not equal to another.

Relational operators are binary meaning they require two operands.

Relational operators have left to right associativity. Left to right associativity means that when two operators of same precedence are adjacent, the left most operator is evaluated first.

Relational OperatorsMeaning
> Greater than
< Less than
>= Greater than or equal to
y

x == y

Note: It can be easy to forget that equal to is “==” and not “=” which is assignment.  In many cases, the compiler won’t note this as an error because x==y and x=y are both valid expressions.

What is the result of a relational expression?

Relational expressions are Boolean expressions and thus are equal to either true or false.

  • 5 > 4

    true

  • 5 == 5

    true

  • 1 < 3

    true

  • 5 != 5

    false

  • 1 > 3

    false

  • 1 >= 1

    true

How a program treats true and false

C++ programs store true and false in memory as numbers.

false is stored as 0

true is stored as 1

Relational operator precedence and relationship to the other operators

Note how the relational operators rank in precedence to the mathematical and assignment operators. Also, note that the less than/greater than operators have higher precedence than the equal/not equal relational operators.

Precedence highest to lowest
[ ]
*, /, %
+, -
>, >=,

Chủ Đề