Skip to main content
Engineering LibreTexts

12.3: Relational Operators

  • Page ID
    10312
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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.

    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)
    Questions
        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.
    

    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

    Definitions

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

    This page titled 12.3: Relational Operators is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee (OpenStax CNX) .

    • Was this article helpful?