Udostępnij za pośrednictwem


Relational Operators: <, >, <=, and >=

expression < expression
expression > expression
expression <= expression
expression >= expression

Remarks

The binary relational operators determine the following relationships:

  • Less than (<)

  • Greater than (>)

  • Less than or equal to (<=)

  • Greater than or equal to (>=)

The relational operators have left-to-right associativity. Both operands of relational operators must be of arithmetic or pointer type. They yield values of type bool. The value returned is false (0) if the relationship in the expression is false; otherwise, the value returned is true (1).

Example

// expre_Relational_Operators.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

int main() {
   cout  << "The true expression 3 > 2 yields: "
         << (3 > 2) << endl
         << "The false expression 20 < 10 yields: "
         << (20 < 10) << endl;
}

The expressions in the preceding example must be enclosed in parentheses because the stream insertion operator (<<) has higher precedence than the relational operators. Therefore, the first expression without the parentheses would be evaluated as:

(cout << "The true expression 3 > 2 yields: " << 3) < (2 << "\n");

The usual arithmetic conversions covered in Arithmetic Conversions are applied to operands of arithmetic types.

See Also

Reference

Expressions with Binary Operators

C++ Operators

Operator Precedence and Associativity

Comparing Pointers Using Relational Operators

C Relational and Equality Operators