Definition of a ‘Variable’

Different languages may differ slightly,

but the concept is the same as shown here.

 

<-- Back

 

 

A “variable” is a letter or word that identifies a place
in the computer’s memory where data is stored.

 

Variable = expression

 

A variable can be used on the left side of the equal sign
in an assignment statement to store information
to be used later in the program.

 

X = 52
Amount = 12.34

A variable can be used on the right side of the equal sign
in an expression to retrieve information
that was previously stored in the computer’s memory.

 

Y = X / 2
Tax = Amount * .0775

 

 

However, sometimes expressions can be more complex

and there is some math to do, like 10 + 15.

 

Now we have to “do the math” and figure it out :

We add the two numbers in the expression to get one answer : 25.

 

Expressions always “resolve” into a single value.

 

Sometimes the math is a little harder to do, like the expression  (7 + 3) * (2 + 3) ,

 

It may be convenient to think of variables as a way to substitute a name for a value.

 

For example :

 

Program Line

What does the program line do ?

 

 

 

Price = 100

 

The program assigns the value 100
to the
variable  Price’
and stores the value 100 in memory.

 

 

 

Tax = .0775

 

The program assigns the value .0775
to the variable  Tax
and stores the value
.0775 in memory. 

 

 

 

 

Total = Price * Tax

 

The program looks up the values
stored at locations in memory

called Price * Tax
and calculates the value of the expression.

The program then assigns the calculated value  
to the variable  Total’

 

 

PRINT Price , Tax, Total

 

Program looks up values

stored in Price , Tax and Total,

and displays or prints them.

 

 

 

<-- Back