Experiment 11: Assignment Expressions


Once we are able to declare variables, the next useful operation is to be able to change the value of those variables. This is most commonly done with an assignment statement:

	Variable = Expression ;
Here, Expression is any valid C++ statement and Variable is an identifier that has been declared as a variable, whose type matches that of Expression. When execution reaches this statement:
  1. Expression is evaluated; and
  2. Its value is stored in the memory associated with Variable.
In C++, the assignment symbol (=) is an actual operator that returns a value. Put differently, the sequence of symbols
   Variable = Expression
is itself an expression that produces some value, just as
   2 + 3
is an expression that produces some value (5). We might determine what value is produced by this operator by performing an experiment that uses an output statement to display that value:
   cout << ( Variable = Expression ) << endl;
(Note that the parentheses are necessary, because = has lower precedence than <<).

Using an experiment like this, determine what value is produced by the assignment operator. Use a variety of values for Expression, and report your findings in the space below:





You should now understand that = is not just a syntactic symbol, but is an operator that produces a value (the value being assigned). We will examine the implications of this idea in the next experiment.


Back to the Experiment List

Forward to the Next Experiment


Copyright 1998 by Joel C. Adams. All rights reserved.