Sams Teach Yourself C++ in 21 Days

(singke) #1
You have explored operator precedence. And you have seen how parentheses can be used
to change the precedence and to make precedence explicit, and thus easier to manage.

Q&A ......................................................................................................................


Q Why use unnecessary parentheses when precedence will determine which
operators are acted on first?
A It is true that the compiler will know the precedence and that a programmer can
look up the precedence order. Using parentheses, however, makes your code easier
to understand, and therefore easier to maintain.
Q If the relational operators always return true or false, why is any nonzero
value considered true?
A This convention was inherited from the C language, which was frequently used for
writing low-level software, such as operating systems and real-time control soft-
ware. It is likely that this usage evolved as a shortcut for testing if all of the bits in
a mask or variable are 0.
The relational operators return true or false, but every expression returns a value,
and those values can also be evaluated in an ifstatement. Here’s an example:
if ( (x = a + b) == 35 )
This is a perfectly legal C++ statement. It evaluates to a value even if the sum of a
and bis not equal to 35. Also note that xis assigned the value that is the sum of a
and bin any case.
Q What effect do tabs, spaces, and new lines have on the program?
A Tabs, spaces, and new lines (known as whitespace) have no effect on the program,
although judicious use of whitespace can make the program easier to read.
Q Are negative numbers true or false?
A All nonzero numbers, positive and negative, are true.

Workshop ..............................................................................................................


The Workshop provides quiz questions to help you solidify your understanding of the
material covered and exercises to provide you with experience in using what you’ve
learned. Try to answer the quiz and exercise questions before checking the answers in
Appendix D, and be certain that you understand the answers before continuing to tomor-
row’s lesson on functions.

96 Day 4

Free download pdf