<< Chapter < Page Chapter >> Page >
An explanation of the assignment operator with examples as used in the C++ programming language.

Discussion

The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within C++ programming language the symbol used is the equal symbol. But bite your tongue, when you see the = symbol you need to start thinking: assignment. The assignment operator has two operands. The one to the left of the operator is usually an identifier name for a variable. The one to the right of the operator is a value.

Simple assignment

int age; // variable set up then later in the programage = 21;

Got questions? Get instant answers now!

The value 21 is moved to the memory location for the variable named: age. Another way to say it: age is assigned the value 21.

Assignment with an expression

int total_cousins; // variable set up then later in the programtotal_cousins = 4 + 3 + 5 + 2;

Got questions? Get instant answers now!

The item to the right of the assignment operator is an expression. The expression will be evaluated and the answer is 14. The value 14 would assigned to the variable named: total_cousins.

Assignment with identifier names in the expression

int students_period_1 = 25; // variable set up with initialization int students_period_2 = 19;int total_students; then later in the programtotal_students = students_period_1 + students_period_2;

Got questions? Get instant answers now!

The expression to the right of the assignment operator contains some identifier names. The program would fetch the values stored in those variables; add them together and get a value of 44; then assign the 44 to the total_students variable.

Definitions

assignment
An operator that changes the value of a modifiable data object.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?

Ask