The Assignment Operator Plus Initilization


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.

Example 1: Simple Assignment

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

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.

Example 2: Assignment with an Expression

int totalCousins;     // variable set up
totalCousins = 4 + 3 + 5 + 2;

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.

Example 3: Assignment with Identifier Names in the Expression

int studentsPeriod1 = 25;     // variable set up
int studentsPeriod2 = 19;
int totalStudents;
totalStudents = studentsPeriod1 + studentPeriod2;

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 totalStudents variable.

There is much more to the assignment operator when it comes to object… We’ll have a look at it in some other blog post.

About Ali Turab Gilani
BS Computer Sciences from G.C University Lahore. Served as Teaching Assistant for Programming Fundamentals... Vice President IEEE Society Student Chapter...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.