Variables are easy! Type this in the body (the place where you type all the commands) of your QBasic window: LET X = 1200 Now press F5. Nothing happens. So, in order to view our variable, we'll have to print it somewhere on your screen. After your LET command, type this: PRINT "The number is "; X
The number is 1200
Is that what you saw on the screen? Good. You're viewing your variable now. Cool, eh? Notice that you didn't put quotation marks around the X in the PRINT command. Why is that, anyway? Well, try this: PRINT "X"...You probably see an X at the top of the screen, right? Well, if you PRINT "X", you are telling the computer to print the letter X on the screen. But, if you PRINT X, you are telling the computer to print your variable that you set with a LET command, and not the letter X. Get it? Also, notice how we combined text and variables to make the message "The number is 1200". A semicolon ";" separates the text from the variables. Neat, huh?
So, now you understand what LET can do. It sets your variable. So why are these variables helpful? Well, the variable we just set is called a NUMERIC variable. It is really a number, and in this case, the number is 1200. So we can tinker with this number as much as we want! Now, take the program you have now, and type this after the two commands we have now:
Okay, at this point in the tutorial, you're probably asking yourself "How can I let the user input his or her own variable?" Well, that's easy too. Start a new program by clicking "File" and then "New". Now type this in:
When you press F5 to run the program, this is probably what you see on the screen...
Will you type a number? _
Now type a number and press enter. This is what you will get:
Will you type a number? 300
300
Notice the second number. With this program, you let the user type in whatever number they wanted to, and the program printed the number right back to them. The INPUT command lets users set their own variables, and the program can use the variables in other points in the program. INPUT lets your users input numbers or text to help make your program easier and more user-friendly.
Text, did you say? How do I make a text variable? Variables that hold text are called STRING variables. STRING variables look just like NUMARIC variables, only they have dollar signs at the end of them. Type this out:
Let's say your name is Jim. Hello, Jim. This is probably what you would see on the screen:
What is your name? Jim
My name is Jim too!
This will come in useful whenever you want to make an interactive program. Your goal is to study these variables and the INPUT commands. Before you leave, maybe you should take a little test. Don't worry, it's only five questions, and they're easy.
Are these variables either numaric, string, or incorrect (not actually variables). Be sure to check your answers or taking the test will be a waste of time.
1) X
NUMARIC
STRING
INCORRECT
2) #WE3
NUMARIC
STRING
INCORRECT
3) NAME$
NUMARIC
STRING
INCORRECT
4) QBASIC
NUMARIC
STRING
INCORRECT
5) @!*$@%
NUMARIC
STRING
INCORRECT