Previous Table of Contents Next


Chapter 4
Expressions and Statements

A program is really nothing more than a set of commands executed in sequence. The magic emerges from the capability to branch from one set of commands to another depending on whether a particular statement is true or false. In this hour you learn

  What statements are
  What expressions are
  How to work with operators
  The meaning of Truth

Statements

All C++ statements end with a semicolon. A statement controls the sequence of execution, evaluates an expression, or does nothing (the null statement).

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

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

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.

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

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:

Add a to b.
Assign the result of the expression a + b to x.
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.

Listing 4.1 Evaluating Complex Expressions


1: #include <iostream.h>
2: int main()
3: {
4:     int a=0, b=0, x=0, y=35;
5:     cout << “a: “ << a << “ b: “ << b;
6:     cout << “ x: “ << x << “ y: “ << y << endl;
7:     a = 9;
8:     b = 7;
9:     y = x = a+b;
10:    cout << “a: “ << a << “ b: “ << b;
11:    cout << “ x: “ << x << “ y: “ << y << endl;
12:    return 0;
13:  }

a: 0 b: 0 x: 0 y: 35
a: 9 b: 7 x: 16 y: 16


Previous Table of Contents Next