C++ defines the value of an assignment expression to be the value of the member on the left, so the expression has the value 20. The fact that assignment expressions have values permits statements such as the following:
maids = (cooks = 4) + 3;
The expression cooks = 4 has the value 4, so maids is assigned the value 7. However, just because C++ permits this behavior doesn’t mean you should encourage it. But the same rule that makes this peculiar statement possible also makes the following useful statement possible:
x = y = z = 0;
This is a fast way to set several variables to the same value. The precedence table (see Appendix D, “Operator Precedence”) reveals that assignment associates right-to-left, so first 0 is assigned to z, and then z = 0 is assigned to y, and so on.
Finally, as mentioned previously, relational expressions such as x < y evaluate to the bool values true or false. The short program in Listing 5.3 illustrates some points about expression values. The << operator has higher precedence than the operators used in the expressions, so the code uses parentheses to enforce the correct order.
Listing 5.3. express.cpp
// express.cpp -- values of expressions
#include
int main()
{
using namespace std;
int x;
cout << "The expression x = 100 has the value ";
cout << (x = 100) << endl;
cout << "Now x = " << x << endl;
cout << "The expression x < 3 has the value ";
cout << (x < 3) << endl;
cout << "The expression x > 3 has the value ";
cout << (x > 3) << endl;
cout.setf(ios_base::boolalpha); //a newer C++ feature
cout << "The expression x < 3 has the value ";
cout << (x < 3) << endl;
cout << "The expression x > 3 has the value ";
cout << (x > 3) << endl;
return 0;
}
Here is the output from the program in Listing 5.3:
The expression x = 100 has the value 100
Now x = 100
The expression x < 3 has the value 0
The expression x > 3 has the value 1
The expression x < 3 has the value false
The expression x > 3 has the value true
Normally, cout converts bool values to int before displaying them, but the cout.setf(ios::boolalpha) function call sets a flag that instructs cout to display the words true and false instead of 1 and 0.
Note
A C++ expression is a value or a combination of values and operators, and every C++ expression has a value.
To evaluate the expression x = 100, C++ must assign the value 100 to x. When the very act of evaluating an expression changes the value of data in memory, we say the evaluation has a
From expression to statement is a short step; you just add a semicolon. Thus, the following is an expression:
age = 100
Whereas the following is a statement:
age = 100;
More particularly, it is an
rodents + 6; // valid, but useless, statement
The compiler allows it, but the statement doesn’t accomplish anything useful. The program merely calculates the sum, does nothing with it, and goes on to the next statement. (A smart compiler might even skip the statement.)
Nonexpressions and Statements
Some concepts, such as knowing the structure of a for loop, are crucial to understanding C++. But there are also relatively minor aspects of syntax that can suddenly bedevil you just when you think you understand the language. We’ll look at a couple of them now.
Although it is true that adding a semicolon to any expression makes it a statement, the reverse is not true. That is, removing a semicolon from a statement does not necessarily convert it to an expression. Of the kinds of statements we’ve used so far, return statements, declaration statements, and for statements don’t fit the
int toad;
But the fragment int toad is not an expression and does not have a value. This makes code such as the following invalid:
eggs = int toad * 1000; // invalid, not an expression
cin >> int toad; // can't combine declaration with cin
Similarly, you can’t assign a for loop to a variable. In the following example, the for loop is not an expression, so it has no value and you can’t assign it:
int fx = for (i = 0; i< 4; i++)
cout >> i; // not possible
Bending the Rules