Commodore 128D User Manual page 47

Hide thumbs Also See for 128D:
Table of Contents

Advertisement

n
Variables
Variables are values that can change over the course of an equation
or program statement. There is a part of the computer's BASIC mem
ory that is reserved for the characters (numbers, letters and sym
bols) you use in your program. Think of this memory as a number of
storage compartments in the computer that store information about
your program; this part of the computer's memory is referred to as
variable storage. Type in this program:
10X = 5
20 ?X
Now RUN the program and see how the computer prints a 5 on your
screen. You told the computer in line 10 that the letter X will repre
sent the number 5 for the remainder of the program. The letter X is
called a variable, because the value of X varies depending on the
value to the right of the equals sign. We call this an assignment state
ment because now there is a storage compartment labeled X in the
computer's memory and the number 5 has been assigned to it. The
= sign tells the computer that whatever comes to the right of it will
be assigned to a storage compartment (a memory location) labeled
with the letter X to the left of the equals sign.
The variable name on the left side of the = sign can be either one or
two letters, or one letter and one number (the letter MUST come
first). The names can be longer, but the computer only looks at the
first two characters. This means the names PA and PART would refer
to the same storage compartment: Also, the words used for BASIC
commands (LOAD, RUN, LIST, etc.) or functions (INT, ABS, SQR, etc.)
cannot be used as names in your programs. Refer to the BASIC
Encyclopedia in Chapter 5 if you have any questions about whether a
variable nameis a BASIC keyword. Notice that the = in assignment
statements is not the same as the mathematical symbol meaning
"equals'-, but rather means allocate a variable (storage compart
ment) and assign a value to it.
In the sample program you just typed, the value of the variable X
remains at 5 throughout. You can put calculations to the right of the
= sign to assign the result to a variable. You can mix text with con
stants in a PRINT statement to identify them. Type NEW and press
RETURN to clear the Commodore 128's memory; then try this
program:
10 A = 3*100
20 B = 3*200
30 ?"A IS EQUAL TO "A
40 ?"B IS EQUAL TO "B
39
USING C128 MODE-Getting Started in BASIC

Advertisement

Table of Contents
loading

Table of Contents