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

Assigning an int division result to a double variable

Activities

[TODO]