Site hosted by Angelfire.com: Build your free website today!
Chapter Three
Tutorials

[WHAT'S NEW] [FAQ] [AWARDS] [TUTORIALS] [GAMES] [LINKS]
[PROJECTS] [CONTESTS] [FEEDBACK]

Alright, QBasic learners, you already know about CLS, PRINT, LOCATE, and SOUND. Now we're going to learn about variables. Variables are very, very important, and you'll use them in nearly every program you make. But don't worry, they're easy to understand. A variable, in QBasic, is something that stands for something else, like a number. These variables can be changed or, well, varied. And the ability to change your variables is about the only reason to have them. So, if you want something to change in your program, variables are your only bet.

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:

LET X = X + 25
PRINT X

Now look at the variable. Open your eyes, Chester! The number you now see is 1225. By doing X = X + 25, you are not only changing the PRINT statement, but you are changing the variable too! Isn't this cool?

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:

INPUT "Will you type a number"; NUM
PRINT NUM

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:

INPUT "What is your name"; name$
PRINT "My name is"; name$; "too!"

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

The answers were: 1) NUMARIC, 2) INCORRECT, 3) STRING, 4) NUMARIC, 5) INCORRECT. Check your answers and see if you did well!
More tutorials coming soon!
Back to Tutorials