Skip to content

Assignment Statement

The assignment statement is an instruction that stores a value in a variable. You use this instruction any time you want to update the value of a variable.

An assignment statements assigns a value to a variable

The assignment statement performs two actions. First, it calculates the value of the expression (calculation) on the right-hand side of the assignment operator (the =). Once it has the value, it stores the value (assigns it) to the variable on the left-hand side of the assignment operator.

Assignment Statement — when, why, and how

When you create a variable, you have identified a piece of information that you want to be able to change as your program runs. Whenever you need to give a variable an initial or new value, you use an assignment statement.

The assignment statement uses the assignment operator =. Whatever is on the right-hand side of = represents the value to be assigned. This could be a literal, a method call, or any other expression. On the left-hand side you write the identifier of the variable you want to store this value in.

For example, you might decide to ask the user for their name. First, you need a variable to store the value. You might decide to call this variable name. Then, the assignment statement lets you read a response from the user and store it in that variable. In this case, the right-hand side of the assignment would be a call to ReadLine, which reads input from standard in and returns it to you. The left-hand side would be the identifier of our variable, name.

It is important to remember that every assignment statement has 2 actions:

  1. Calculate the value on the right-hand side
  2. Store it in the variable on the left-hand side.

The ordering of these actions allow you to update the value of a variable using an expression involving the variable being updated. This can be very useful. For example, you might want to update the value of a variable storing the number of steps you have taken today.

In C#

In C# the assignment operator is =. Most assignment statements are written using =, with an identifier on the left-hand side and an expression on the right-hand side. The assignment operator can optionally be modified with +, -, *, or /, which are shorthands for adding to, subtracting from, multiplying, and dividing the variable identified on the left-hand side of the statement.

Some assignment statements are written without =. These are assignment statements using increment (++) or decrement (--), which allow you to add or remove one from a variable’s current value.

For example, x = x - 1, x -= 1, and x-- are all assignment statements which do the same thing — assign the variable x a new value that is one lower than its current value.

Examples

Basic assignment statement

In this example we use ReadLine to get input from the user and store it in a name variable.

using static SplashKitSDK.SplashKit;
// Declare a variable - somewhere we can store a string
string name;
Write("Enter your name: ");
// Assign a value to name
//
// name is the left-hand side - where to store the value
// |
// | ReadLine() is the right-hand side. It is called
// | | to get a value to assign to name
// v v
name = ReadLine();

Shorthand assignment statements

The following code shows an example of how to use some of the shorthand assignment statements.

using static System.Convert;
using static SplashKitSDK.SplashKit;
string line;
int count;
Write("What is the start count: ");
// Read in a line from the user
line = ReadLine();
// Initialise the count to a user-provided value.
count = ToInt32(line);
WriteLine($"Count is {count}");
// Add one to count
//
// count is the left-hand side - where to store the value
// |
// | count + 1 is calculated first
// | |
// v v
count = count + 1;
WriteLine($"After count = count + 1...count is now {count}");
// We can use shorthand to do this too
//
// count is the left-hand side - where to store the value
// |
// | This is the same as count = count + 5
// | |
// v v
count += 5;
WriteLine($"After count += 5...count is now {count}");
// We can make this shorter for + 1
//
// count is the left-hand side - where to store the value
// |
// | This is the same as count = count + 1
// | |
// v v
count++;
WriteLine($"After count++ ...count is now {count}");

If you ran the above code and entered 17 as the start count you should get this output:

Terminal window
What is the start count: 17
Count is 17
After count = count + 1...count is now 18
After count += 5...count is now 23
After count++...count is now 24

You do not always need to store values in variables. Sometimes you can just use the value and then forget it. For example, in the above code, we read the initial count from the user. This requires us to read it as text, and then convert that text to a number. Given that we do not ever use the details in line again, we do not need to create this variable in the first place. Instead, we could pass the value to the convert function directly as shown below.

using static System.Convert;
using static SplashKitSDK.SplashKit;
int count;
Write("What is the start count: ");
// Initialise the count to a user provided value.
//
// Instead of storing in line and using it here, we can call
// ReadLine and pass its result to ToInt32 directly.
// |
// v
count = ToInt32(ReadLine());
//... the rest of the code would be the same

Assignment statement up close

The following sliders show how the assignment statement works in detail. These are both relatively simple programs, but notice how much is going on behind the scenes!

Assigning an int division result to an int variable

Assignment for integer division
The variable quotient is allocated memory on the stack
1 is added to the program counter - it is now at instruction 2. Instructions on line 1 are now finished... line 2 is about to start
Line 2 also allocates memory on the stack for the two variables 'dividend' and 'divisor'
1 is added to the program counter again - line 2 is done, line 3 is starting
Line 3 is an assignment - it involves evaluating the expression (3a) and saving the result (3b). These involve distinct instructions, but we will update the program counter with letters
Instruction 3a will evaluate the right-hand side...to evaluate - 21 is loaded into the CPU - the compiler decided to put it in register 1
1 is added to the program counter... but we will call it '3b'
3b saves the result of the expression to 'dividend'. The value is in Register 1, 3b copies it to memory. So 21 is saved to the location of the dividend variable
The last step of the instruction on line 3... add one to the program counter... on to line 4!
Line 4 is also an assignment statement - and again involves evaluating the expression (4a) and saving the result (4b)
Let's run through it quickly: 4a: loads 4 into Register 1 (replacing the old value 21). 4b: save the result (in Register 1) into the 'divisor' variable on the stack memory. 4c: The program counter is incremented to line 5
How does the division operator work? Again, remember, assignment = two actions: evaluate (the right hand side expression) and save (into the left hand variable). For division, evaluate will need a few actions - Load the variables (5a and 5b) and do the division (5c)
Let's consider 5a and 5b in the same step - 5a: Load the divisor value (4) into Register 1. 5b: Load the dividend value (21) into Register 2
The CPU does the division (Register 1 / Register 2). This is integer division, so 21/4 = 5. The result of this instruction is stored in it's own register (Register 3 here)
Step 5d is the save part of the assignment. The result is in Register 3 - so it is saved to the 'quotient' variable in the stack memory. Assignment done... add one to the program counter !
Argh... lots of steps here ! We must evaluate the arguments to pass to WriteLine. 6a: Load dividend 6b: Convert to string 6c: Load '/' 6d: Append 6e: Load divisor 6f: Convert to string 6g: Append 6h: Load '=' 6i: Append 6j: Load quotient 6k: Convert to string 6l: Append. Result is '21/4=5'. Result passed to WriteLine(k) which outputs it to the terminal
The procedure call ends, so we add one to the program counter... ending our program

Assigning an int division result to a double variable

Assignment for a division stored in a double.
The right hand.
The result of the evaluation of the right hand side, is saved into the variable on the left hand side of the assignment operator.
the CPU increments the program counter ready for the next instruction

Activities

[TODO]