C++20 sections · 1024 units
Open in Course

Less/Greater or Equal

Inclusive bounds

The <= operator returns true if the left side is less than or equal to the right. if (x <= 5) passes when x is 5 or smaller. These operators prevent off-by-one errors in range checks.

To include values from 0 to 10, use if (x >= 0 && x <= 10). Using < would exclude 10. Equality is checked first. x <= y evaluates as (x < y) || (x == y). If the first is true, the second never runs.