Programming Fundamentals/Relational Operators

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Overview[edit]

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[edit]

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[edit]

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

References[edit]