This page is optimized for mobile devices, if you would prefer the desktop version just click here

11.2 Relational operators

An introduction to the six common relational operators used in programming.

Overview of the relational operators

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.

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

Operator symbols and/or names vary with different programming languages. The C++ programming language operators with their meanings are:

C++ Operator Meaning
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equality (equal to)
!= inequality (not equal to)

    Evaluate the following boolean expressions:

  1. 9<25
  2. 9<3
  3. 9>14
  4. 9<= 17
  5. 9>= 25
  6. 9 == 13
  7. 9 != 13
  8. 9 !<25

    Answers:

  1. 1
  2. 0
  3. 0
  4. 1
  5. 0
  6. 0
  7. 1
  8. Error, the "not less than" is not a valid operator.
Got questions? Get instant answers now!

The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false.

Be careful.  In math you are familiar with using this symbol = to mean equal and ≠ to mean not equal.  In the C++ programming language the ≠ is not used and the = symbol means assignment.

Demonstration program in c++

Creating a folder or sub-folder for source code files

Depending on your compiler/IDE, you should decide where to download and store source code files for processing. Prudence dictates that you create these folders as needed prior to downloading source code files. A suggested sub-folder for the Bloodshed Dev-C++ 5 compiler/IDE might be named:

  • Demo_Programs

If you have not done so, please create the folder(s) and/or sub-folder(s) as appropriate.

Download the demo program

Download and store the following file(s) to your storage device in the appropriate folder(s). You may need to right click on the link and select "Save Target As" in order to download the file. Following the methods of your compiler/IDE, compile and run the program(s). Study the source code file(s) in conjunction with other learning materials.

Download from Connexions: Demo_Relational_Operators.cpp

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.