Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Less-than-or-equal operator (<=)

The <= operator checks whether its left operand is less than or equal to its right operand. The built-in version returns bool. It is useful for inclusive limits; take care with type conversions and chained comparisons.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

lhs <= rhs

For numbers, lhs <= rhs tests the “less than or equal to” relationship after the usual arithmetic conversions. The built-in operator returns bool: true or false. Mixing signed and unsigned types can change the compared values through conversion.

Relational operators group left-to-right. Do not express a range as low <= value <= high: it means (low <= value) <= high, so the second comparison uses a Boolean result. Write low <= value && value <= high instead.

For pointers, ordering is specified in cases including elements of the same array. Do not assume a portable ordering of arbitrary unrelated pointers: the result can be unspecified. For class types, meaning can come from an overloaded operator<= or selection of a candidate rewritten from operator<=>; an overload need not return bool.

The example covers a value below the limit, equal to it, and above it. This entry targets C++23; the referenced document is an evolving working draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    const int limit = 10;
    std::cout << std::boolalpha;
    std::cout << (9 <= limit) << '\n';
    std::cout << (10 <= limit) << '\n';
    std::cout << (11 <= limit) << '\n';
}

Result

true
true
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources