Chapter 4: Relational Operators

Relational Operators

russian dolls of various sizes 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 Operators Meaning
> 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
  • 5 == 5
  • 1 < 3
  • 5 != 5
  • 1 > 3
  • 1 >= 1

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; 

program output

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 <cctype>), you can access the following character testing functions which all return true or false.

Character function Description
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