Relational and Logical Operators¶
Relational Operators (1)¶
Operator |
Meaning |
Operand type |
---|---|---|
|
greater |
integer, floatingpoint |
|
greater equal |
integer, floatingpoint |
|
less |
integer, floatingpoint |
|
less equal |
integer, floatingpoint |
|
equal |
integer, floatingpoint |
|
not equal |
integer, floatingpoint |
Attention
==
and !=
is legal for floatingpoint numbers, but not
what you want!
Relational Operators (2)¶
Precedence rules
All relational operators are preceded by arithmetic operators
>
,>=
,<
,<=
==
,!=
Note
Operators with equal precedence are associated from left to right
So what does that mean?
|
Arithmetic has precedence |
|
It is true that ‘X’ is not ‘U’ |
|
Same, explicitly precedented |
|
It is true that 3 is not less than 1 |
|
What?! |
Logical (Boolean) Operators¶
Logical expressions
|
AND |
|
OR |
Precedence rules
Boolean operators bind less strong than relational and arithmetic operators
&&
precedes||
Operators with equal precedence are associated from left to right
Boolean Operators: Short-Circuit¶
Short-circuit calculation
Boolean expressions are only evaluated to the point where their truth value is known
⟶ Elegant and (for beginners at least) unreadable constructs
int c, num_lf = 0;
while ((c = getchar()) != EOF && c == '\n' && ++num_lf);