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 remainder (%) operator returns the remainder from an integer division.
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)
b) (4 > 6 && true)
c) (3 >= 3 || false)
d) (true || false) ? 4 : 5
Question #3
Why should you never do the following:
a) int y{ foo(++x, x) };
b) double x{ 0.1 + 0.1 + 0.1 }; return (x == 0.3);
c) int x{ 3 / 0 };