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.
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:
- Calculate the value on the right-hand side
- 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.
Shorthand assignment statements
The following code shows an example of how to use some of the shorthand assignment statements.
If you ran the above code and entered 17 as the start count you should get this output:
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.
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]