< previous page page_42 next page >

Page 42
A common simple statement is an assignment:
x = a + b;
Unlike in algebra, this statement does not mean that x equals a+b. This is read, Assign the value of the sum of a and b to x, or Assign to x, a+b. Even though this statement is doing two things, it is one statement and, therefore, has one semicolon. The assignment operator assigns whatever is on the right side to whatever is on the left side.
Whitespace
New Term: Spaces, along with tabs and new lines are called whitespace. Extra whitespace is generally ignored by the compiler; any place you see a single space you can just as easily put a tab or a new line. Whitespace is added only to make a program more readable by humans; the compiler won't notice.
The assignment statement previously discussed could be written as
x=a+b;
or as
x                      =a
+           b           ;
Although this last variation is perfectly legal, it is also perfectly foolish. Whitespace can be used to make your programs more readable and easier to maintain, or it can be used to create indecipherable code. In this as in all things, C++ provides the power; you supply the judgment.
Compound Statements
New Term: Any place you can put a single statement, you can put a compound statement. A compound statement begins with an opening brace ({) and ends with a closing brace (}).
Although every statement in a compound statement must end with a semicolon, the compound statement itself does not end with a semicolon. For example, consider this piece of code:
{
    temp = a;
    a = b;
    b = temp;
}
This compound statement swaps the values in the variables a and b.

 
< previous page page_42 next page >

If you like this book, buy it!