The "Assignment" Statement
< Back
>
Assigns a value
to a variable to be stored in memory, for use later in a program
To use an assignment statement in a program to store
some value :
1.
write the variable you want,
2.
type the equal sign (=)
3.
then type value or expression
you want to save
i.e: y = 5 + 2
The program will evaluate
the expression
on
the right side of the equal sign (=)
and store that value into
the variable on the left side of the equal sign.
Assignment
statements like x = 12" and "y = 7" store values into the computer's memory
so that the program can use them later.
Below
is an example showing both of the statements being executed
and evaluating the expression in a PRINT statement :
Program to add 2 values stored in variables
:
x = 6 + 6 ' x is the variable you want
to have the value 12 (the program adds 6 + 6 and stores the result)
y
= 5 + 2 ' y is the
variable you want to have the value 7
(the program adds 5 + 2 and stores the result)
PRINT
x + y ' Look up whats stored in x and y, add them
together to display the answer
After
assigning values to x and y,
the program can PRINT
the sum of the values that are stored there.
Since
x has the value 12 (6 + 6) and y has the value 7 (5 + 2),
the PRINT statement displays the answer : 19 on the display screen.
Example :

The
statement Total = UnitPrice * Quantity
may look like
an algebraic statement
of equality,
but is actually an assignment statement.
These are not the same
thing.
The difference is that, in algebra this
statement states that
the value on the right of the equal sign
is
equal to the value
on the left of the equal sign.
In programming languages, this
statement
assigns
the value of the expression on the right of the equal sign
to the memory location defined by the variable
on the left of the equal sign.
<
Back >