So far our programs have only talked to us. Let's
write one that will listen. Get rid of the previous
program by clicking on File, then New on QBASIC's
menu. Click on < No > when it asks if you want to
save the old program now. Try this:
CLS INPUT "Enter your name: ", Name$ PRINT "Hello, "; Name$; ". How are you today?"
Don't forget the comma (,) between "Enter your
name: " and Name$. Run it. When it asks, type your
name, then press the <Enter> key.
What's in a "Name$"?
"Name$" is called a "variable". To be variable
means that things can change. Try running the
program again, but this time type in a friend's
name (don't forget the <Enter> key). Sure enough,
the message changes.
INPUT
INPUT Name$ takes what you type at the keyboard
and puts it into the Name$ variable. PRINT Name$
prints out what is in the Name$ variable.
Variables
Variables hold letters and numbers. The dollar
sign ($) means this variable can hold letters.
These are called "string variables".
Variables without a dollar sign can only hold
numbers. We'll be seeing them soon.
You can call your variables anything you want.
Try going back through this program and changing
every "Name$" to "Fred$". What happens when you
run it?
Another way to think of a variable is to imagine
a small bucket with a name on it. Put
"Name$" on it. This is the bucket's (variable's) name.
Now take a piece of paper
and write your name on it and drop it into the
imaginary bucket. Now the variable Name$ has your
name in it. Computer variables can only hold
one piece of paper (one value) at a time.
PRINT and Variables
When you want to PRINT what's in a variable, leave
off the double-quotation marks ("). This program will
show you how this works:
CLS INPUT "Enter your name: ", Name$ PRINT "Name$" PRINT Name$
The first PRINT statement prints Name$ on the screen.
The second PRINT statement prints whatever name you
entered.
Learned
Source: http://jpsor.ucoz.com |