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?

3 - 1 == 2

Arithmetic has precedence

'X' != 'U' == 1

It is true that ‘X’ is not ‘U’

1 == ('X' != 'U')

Same, explicitly precedented

3 < 1 == 0 == 1

It is true that 3 is not less than 1

0 == 1 < 2

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

Counting leading blank lines
int c, num_lf = 0;

while ((c = getchar()) != EOF && c == '\n' && ++num_lf);