5.4.1 Comparisons

a == b

True if a equals b

a != b

True if the operands are not equal.

a < b

True if a is less than b.

a <= b

True if a is less than or equal to b.

a > b

True if a is greater than b.

a >= b

True if a is greater than or equal to b.

Logical operations

!expr

True if expr is false.

a && b

Logical and: true if both a and b are true, false otherwise.

a || b

Logical or: true if at least one of a or b are true.

Both logical ‘and’ and ‘or’ implement boolean shortcut evaluation: their second argument (b) is evaluated only when the first one is not sufficient to compute the result.

Precedence

The following table lists all operators in order of decreasing precedence:

Operators Description
(...) Grouping
? Ternary operator
** Power (right-associative)
- Unary negation
* / Multiplication, division
+ - Addition, subtraction
< <= >= > Relational operators (non-associative)
== != Equality comparison (non-associative)
! Boolean negation
&& Logical ‘and’.
|| Logical ‘or

When operators of equal precedence are used together they are evaluated from left to right (i.e., they are left-associative), except for comparison operators, which are non-associative and for the power operator, which is right-associative (these are explicitly marked as such in the above table). This means, for example that you cannot write:

 
(5 <= x <= 10) ? x : y

Instead, you should write:

 
(5 <= x && x <= 10) ? x : y