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 what’s stored in x (12) and what’s stored in y (7), add them together to display the answer (19)

 

 

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.

 

NOTE:

In C and C++, the assignment uses a single “=” (x = 5) to set x equal to some value.
and the “
comparison” (“if equal equals y”) uses “==” :

// if x is equal to 5, then do something:
If (x == 5) {do something};

 

 

NOTES:

Programming : Assignment Statement

In programming languages, the statement “X = 5” is an instruction :
This looks up the value on the right side of the equal sign (5)
and then assigns (stores) it into the variable on the left side of the equal sign.

 

5 = X is NOT a valid assignment statement
(variable must be on left side)

 

 

 Algebraic : Statement of Equality

The “equal sign” means that the values on

both sides of the equal sign are equal to each other. 

 

X = Y is the same as Y = X

 

5 = X is a VALID assignment statement
(variables or numbers can be on either side)

 

 

 

 

< Back >