Skip to main content
Engineering LibreTexts

6.4: Relational Operators

  • Page ID
    29060
  • \( \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)
    #include<iostream>
    using namespace std;
    
    // Main function
    int main()
    {
        int num1, num2;
        num1 = 33;
        num2 = 99;
    
        if(num1 != num2)
            cout << num1 << " is not equal to " << num2 << endl;
    
        if(num1 >= num2)
            cout << num1 << " is greater than " << num2 << endl;
        else
            cout << num1 << " is smaller than " << num2 << endl; 
        return 0;
    } 
    

    The answers to Boolean expressions within the C++ programming language are a value of either 1 for true or 0 for false. So, when we look at the first condition, it evaluates the 2 values, using the relational operator, and then returns a value of true , 1, or false, 0. This then determines whether the if statement is true or false. If we were using string variables, the comparison are based on the numerical value of the letters.

    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.

     

    Adapted from: "Boolean Data Type" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2 is licensed under CC BY 4.0 

     

    This page titled 6.4: Relational Operators is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.