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
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equality (equal to)
!= or <> inequality (not equal to)

Examples:

  • 9 < 25
  • 9 < 3
  • 9 > 14
  • 9 <= 17
  • 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

Operators are used to determine whether a specific relationship exists between two values
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
<= Less than or equal to
== Equal to
!= Not equal to

Relational Expression Examples

x > 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
( )
*, /, %
+, -
>, >=, <, <= relational
==, != relational
=, *=, /=, %=, +=, -=

An example showing precedence

Since relational expressions are Boolean expressions, I have assigned the results of the expression to a bool data type variable. As noted in the example, programmers often enclose relational expressions in parentheses to improve readability.

int x = 5;
int y = 10;

bool result;

// Assign result of relational expression to bool type variable
result = x > y; // result is false

cout << "x > y: " << result << endl; 

result = x + 10 > y / 5; // result is true

// equivalent, parentheses are not needed but adds readability here
result = (x + 10) > (y / 5); 

// bool variable is output as 0 or 1 instead of true or false
cout << "(x + 10) > (y / 5): " << result << endl; 

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

Comparing characters with relational operators

As discussed earlier, characters are stored in memory as integers.  Thus, you can compare characters using relational expressions just like they were numbers.

char testCharacter = 'c';

if (testCharacter == 'c')
  cout << "testCharacter is c" << endl;

if (testCharacter == 'C')
  cout << "testCharacter is C" << endl;

The > and < operators are sometimes useful with characters.  The integer values of the characters ‘a’ to ‘z’ range from 97-122 and the integer values for ‘A’ to ‘Z’ range from 65-90.  So ‘a’ < ‘b’ is true and ‘A’ > ‘a’ is false.

Comparing strings with relational operators

You can also compare strings using relational operators. When a relational operator is used with strings, the integer value of each character of the left operand is compared to the integer value of each character of the right operand working from left to right.

“tim” == “tom” // false because the 2nd characters i and o do not match

“Tim” == “tim” // false because T and t do not match

“abc” < “abd” // true because for 3rd character, the integer value of c is less than the integer value of d

Character testing functions

If you include the cctype header file in your program (#include ), you can access the following character testing functions which all return true or false.

Character functionDescription
isalpha true if letter in alphabet
isdigit true if digit from 0-9
isalnum true if letter in alphabet or numerical digit
islower true if lowercase letter
isprint true if printable character including space
ispunct true if printable character other than digit, letter, or space
isupper true if uppercase letter
isspace true if character is whitespace (tab, vertical tab, space, or newline

All of these functions have a single character parameter so you can use them like this

isdigit('a') // false
isalpha('a') // true
isupper(myCharVariable)

Which operator is used to determine that the operands are not exactly of the same value?

The inequality operator != returns true if its operands are not equal, false otherwise. For the operands of the built-in types, the expression x !=

When you use the relational operators you can compare quizlet?

Relational operators allow you to compare numeric and char values and determine whether one is greater than, less than, equal to, or not equal to another. > < >= (greater than or equal to) <= (less than or equal to) == (equal to) != (not equal to).

What type of operators are the following ==?

Relational Operators == (Equal to)– This operator is used to check if both operands are equal.

Is used within a simple condition to compare two values?

A logical operator is used in Excel to compare two values. Logical operators are sometimes called Boolean operators because the result of the comparison in any given case can only be either TRUE or FALSE.