- Learn C++ - https://www.learncpp.com -

5.x — Chapter 5 summary and quiz

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.

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 #2

Evaluate the following:

a) 7 / 4

b) 14 % 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; if (x == 0.3) return true; else return false;

c) int x = 3 / 0; O.1 -- Bit flags and bit manipulation via std::bitset Index 5.7 -- Logical operators