< previous page page_43 next page >

Page 43
DO
DO use a closing brace any time you have an opening brace.
DO end your statements with a semicolon.
DO use whitespace judiciously to make your code clearer.

Expressions.
New Term: Anything that returns a value is an expression in C++.
There it is, plain and simple. If it returns a value, it is an expression. All expressions are statements.
The myriad pieces of code that qualify as an expression might surprise you. Here are three examples:
3.2                         // returns the value 3.2
PI                          // float const that returns the value 3.14
SecondsPerMinute            // int const that returns 60
Assuming that PI is a const equal to 3.14 and SecondsPerMinute is a constant equal to 60, all three of these statements are expressions.
The complicated expression
x = a + b;
not only adds a and b and assigns the result to x, but returns the value of that assignment (the value in x) as well. Thus, this statement is also an expression. Because it is an expression, it can be on the right side of an assignment operator:
y = x = a + b;
This line is evaluated in the following order:
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
Add a to b.
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
Assign the result of the expression a + b to x.
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
Assign the result of the assignment expression x = a + b to y.
If a, b, x, and y are all integers, and if a has the value 2 and b has the value 5, both x and y will be assigned the value 7. Listing 4.1 illustrates evaluating complex expressions.

 
< previous page page_43 next page >

If you like this book, buy it!