Quick review

Always use parentheses to disambiguate the precedence of operators if there is any question or opportunity for confusion.

The arithmetic operators all work like they do in normal mathematics. The modulus (%) operator returns the remainder from an integer division. Prior to C++11, beware of integer division and modulus using negative numbers.

The increment and decrement operators can be used to easily increment or decrement numbers. Avoid the postfix versions of these operators whenever possible.

Beware of side effects, particularly when it comes to the order that function parameters are evaluated. Do not use a variable that has a side effect applied more than once in a given statement.

The comma operator can compress multiple statements into one. Writing the statements separately is usually better.

The conditional operator is a nice short version of an if-statement, but don’t use it as an alternative to an if-statement. Only use the conditional operator if you use its result.

Relational operators can be used to compare floating point numbers. Beware using equality and inequality on floating point numbers.

Logical operators allow us to form compound conditional statements.

Quiz time

Question #1

Evaluate the following:

a) (5 > 3 && 4 < 8)

Show Solution ^{[1]}

b) (4 > 6 && true)

Show Solution ^{[1]}

c) (3 >= 3 || false)

Show Solution ^{[1]}

d) (true || false) ? 4 : 5

Show Solution ^{[1]}

Question #3

Why should you never do the following:

a) int y = foo(++x, x);

Show Solution ^{[1]}

b) double x = 0.1 + 0.1 + 0.1; if (x == 0.3) return true; else return false;

Show Solution ^{[1]}

c) int x = 3 / 0;

Show Solution ^{[1]}

O.1 -- Bit flags and bit manipulation via std::bitset ^{[2]} |

Index ^{[3]} |

5.7 -- Logical operators ^{[4]} |