Apple IIe User Manual
Page 32
Page 32 of 74
IIe
Printed: Tuesday, March 4, 2003 10:40:15 AM
question, like:
10 PRINT WHAT'S YOUR NAME?
The next line of the program is an INPUT statement that includes a variable, like:
20 INPUT N$
(The dollar sign tells the program that the input will be a word.
If the input will be a
number, you leave the dollar sign off.)
Try typing the first two lines of this little program:
NEW 10 PRINT WHAT'S YOUR NAME? 20 INPUT N$
Now run the program and type a response to the question that appears on the screen.
Type
RUN
The name you typed is now stored under the variable N$.
Watch what happens when you include
the variable N$ in a PRINT statement later in the program.
Type this:
30
PRINT NICE TO MEET YOU N$
(Don't forget the space after YOU, and make sure that the variable N$ is outside the quotation
marks.)
Then type
40 END
Now run the program.
If all goes well, the program should ask you for your name, then use your
name in place of the variable N$, like this:
WHAT'S YOUR NAME? ?NANCY NICE TO MEET YOU NANCY
INPUT statements let you define variables from the keyboard.
(In other words, the user's typed
input becomes the value of the variable.)
But sometimes you need to define a variable from
within the program, as in the following example:
NEW 10 PRINT HOW OLD ARE YOU? 20 INPUT A 30 LET D = A * 365 40 PRINT THAT MAKES YOU
APPROXIMATELY D 50 PRINT DAYS OLD! 60 END
In this example, LET defines the variable D as the user's response to the question How old are
you?
times 365 the number of days in a year.
(This simple program assumes everybody was born
on January 1st.
When you become more experienced at programming, you can calculate the exact
number of days including extra days for leap years!)
Feel free to type the age program and try it on your friends.
(Don't forget to press Return at
the end of each line.)
For a challenge you might want to change it so it calculates a person's
age in months instead of days!
Don't forget to type NEW before you enter your new program.
Naming Variables
You can use single letters like A, D, and N$ as variables, or you can use whole words like AGE,
DAYS, and NAME$.
The advantage of using whole words is that it's easier to remember what they
represent.
The only drawback to using words as variables is that BASIC uses only the first two
characters of a variable name to distinguish one variable from another.
To BASIC, the variable
DAYS is the same as the variable DARK, so be sure the first two characters of your variables
are unique.
BASIC Reserved Words:
As you're writing programs that use variables, avoid the following words and abbreviations and
any words that include these words, like LETTER. (They're either Applesoft BASIC commands or
ProDOS commands.
If you forget and use one of these commands as a variable by mistake, BASIC
will try to use it as a command with unfortunate results.