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:
Variable = Expressionis itself an expression that produces some value, just as
2 + 3is 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.
Forward to the Next Experiment