What you will learn in this lesson
- What Variables are all about: Strings and Numericals
- Subsituting Variables: Sleep x%, Print x$, y% + 2
- Program Flow: If, Then, Else If
- Directional Input: Input, Line Input, (,), (;)
Welcome to Lesson 2 of Module 1! What are we going to learn today? Well, today we will be learning about what makes QBasic tick: Variables.
Remember your Science lessons back in the 3rd grade? Remember what your teacher said about independent variables and dependent variables? If you slept through your lessons because the teacher was so boring, too bad for you.
Variables are basically something like algebric terms: x and y, except that they can be more than one character long, e.g. apple% or brownbear. Think of them as "containers" that store a
defined value or amount. In QBasic there are three kinds of variables: ordinary, string and numerical.
The main difference between these variables is in the types of information they hold. String variables must be followed by a pair of inverted commas ("") and they hold only text. Ordinary and Numerical variables hold only numbers. The contents of different variable types are not interchangable.
For example, you can't hold text in an ordinary variable; you must use a string variable. And you can't do mathematical calculations with
a number "2" stored in a string variable; you must have it stored in either an Ordinary or Numerical variable.
Variable types, whether string, numerical or ordinary are defined by the sign at the end of the variable name. We call this sign the signature. Basically there are 3 kinds of signatures, one for each type of variable.

String variables: $ - e.g. apple$, poohbear$, piglet$
Ordinary variables: (no signature) - e.g. owl, name, value
Numerical variables: % - scorevalue%, testvalue%
Think of a new variable as an empty container. For an empty variable to be any good to you, you must first fill it, right? This process of "filling" the variable is called defining it. Defining a variable is very simple. All you have to do is to type the name of the variable and its signature, then type =, and then type in the value you want to fill the variable with. If it is a string variable, you must include inverted commas. For example:
poohbear$ = "My name is Winnie the Pooh. I love honey."
pigletsbirthday = 2908
gamescore% = 3200
Now the string variable poohbear$ contains the value "My name is Winnie the Pooh. I love honey." Similarly, the variables
pigletsbirthday and the numerical variable gamescore% contain the values 2908 and 3200 respectively. So, its quite simple to define variables. All you need is the = sign!
Please Note: A common error is to write the value first before the variable. Remember, this is not Math, this is QBasic! For example: 1 + 2 = number1. This is wrong. It should be number1 = 1 + 2. Be careful not to make this mistake!
So you've learnt how to name variables, distinguish between different variable types, how to define them and that they are not interchangable. Not bad for a day. . .
Be careful with different variable types. A common pitfall of newbie programmers is to store a numerical value e.g. "3" in a string variable and then try to use the string variable in sums. Remember, QBasic will treat the "3" in the string variable as text and not as a value!!! Here is a code sample that will illustrate what in the world I am talking about:
number1$ = "2"
number2$ = "4"
number1$ + number2$ = number3$
PRINT number3$

Of course, this is hopelessly wrong and you will get an error message. Why?
The Programmer thinks: number1$ has a value of 2, right? And number2$ has a value of 4, right? 2 and 4 are numbers, right? So you can add them together, right?
But QBasic thinks: number1$ and number2$ are text, right? As they have got a $ after number1 and number2, right? But you can't add text together, right? And thus QBasic gives you an error message. (Right!) I hope you understand what's going on as this is very important. The correct way to do it is:
number1 = 2
number2 = 4
number1 + number2 = number3
PRINT number3
In this case, you can see that Ordinary variables are used and thus QBasic recognizes the variables as a value and not as text. So do you understand this?
Erasing Variables with Kill
The last thing you need to know about variables is how to erase them from your program. But why, you may ask, why do we need to erase variables? Why do we need to cause so much pain and suffering to the little things? Well, think of them as weeds. Variables take up precious memory space when loaded.
And QBasic only has the 640K of conventional memory that Dos Programs get. Now, if you are programming something huge, you may find that you don't have enough memory.
If you don't kill those variables that you don't need, less and less memory will be left available, and you might get Out Of Memory errors in your program. So how do you put the poor guys out of their suffering? How do you spray the weedkiller? The command for this is Kill.
(Oooh! How violent!) Kill can also be used to erase files, but that will be explained later in Lesson 5. For now, the syntax for erasing a variable is:
KILL variablename$
That's about all you need to know about variables.
P.S - It's a very good idea to name your variables something which you can recognize easily. For example, don't name a string variable which contains the user's name Barney-the-dinosaur$ or Poohbear-piglet-christopherrobin-and-owl-on-a-really-blustery-day$. This is especially important when you are programming huge programs, to aid in programming.
Subsituting Variables
What in the world does subsituting variables mean? It sounds complicated. Actually, subsituting variables is a very useful feature in QBasic. It allows you to use variables in place of numbers for certain commands. The code below will show you what I am talking about.

INPUT "Please enter your preferred text delay, in seconds. ", textdelay
PRINT "Hi, I'm Winnie the Pooh Bear!"
SLEEP textdelay
PRINT "I love Honey!"
SLEEP textdelay
PRINT "Piglet is my best friend!"
SLEEP textdelay
END

Please enter your preferred text delay, in seconds. _
(A prompt appears and the program freezes)
Hi, I'm Winnie the Pooh Bear!
(The program freezes)
I love Honey!
(The program freezes)
Piglet is my best friend!
(The program freezes)
What a wonderful story about Pooh Bear! Well, this program here uses the Input command. You will learn it later, but all you need to know about it to understand this program is that it gets a value from the user and stores it in a variable.
The variable in question here is textdelay. Note the syntax of the Sleep command here: instead of Sleep (a number), it is Sleep textdelay, which is a variable. This is what I mean by
Subsituting Variables.
For certain commands, variables can be used instead of specific values. This will allow the user to customize his program. For example, when you are programming an RPG, you will want to display character conversations, right? But you're not sure how fast the user likes his text displayed. So you ask him using the Input command and store it in a variable. Then you
use Variable Subsitution and the Sleep command to pause the thing during conversations as long as HE likes it, not as long as YOU like it.
Thus this is a good way to allow users to customize their programs. Later, when you learn FAT Commands in Lesson 5, you will find this very useful, especially with the Chdir command, which will change the QBasic working directory. You can even store these variables in an .INI file when you learn File Commands! But for now, all you
need to do is to understand that variables can be used instead of values for certain commands. Furthermore, for some other commands such as Locate, both (!) can be used together! For example:
Locate (A% + 2), (B% + 3)
Thus here the program will add the variable A% with 2 to get coord 1, and B% with 3 to get coord 2! How marvellous!
Please Note: Most commands using variable subsitution require a specific kind of variable. For example, the Chdir command (covered in Lesson 5) only accepts String Variables, but the Print command can accept both String and Normal variables, and the Sleep command accepts only Normal variables.
Directional Input

Program Flow, the topic after Directional Input, is how QBasic interprets your program. It is very closely related to the topic at hand, which is obviously Directional Input. Usually, QBasic will go down line by line, until it reaches the end of the program. That is Program Flow. However, it is possible to change the order or sequence in which QBasic interprets these commands using input from the user. This
input is usually in the form of values stored in variables, and that is why we decided to put these topics into the same Lesson.
Directional Input is the input from the user. This input has the "power" to change the Program Flow in the program, thus it is called Directional (as it changes the direction).
The first commands that we are going to teach you deal with how to let the user input values into variables. These are the Input, Line Input, and the use of the (,) and (;) signs. Naturally we will also teach you the basic commands to control Program Flow: If, Then, and Else If. More advanced commands will be covered in Lesson 3.
Let's start off with the Input commands. Look at the three programs below.
(The first Program)
Screen 12: Color 15: Cls
Input name$
(The second Program)
Screen 12: Color 15: Cls
PRINT "Hi! My name is Barney the Dinosaur!
INPUT "Tell me your name! "; name$
(The third Program)
Screen 12: Color 15: Cls
Input "Barney: Can you tell me your name? ", name$
Print "Barney: Oooh! "; name$; " is such a lovely name!"
Print "Barney: "; name$ ", have I told you lately"
PRINT "how much I love you?"
(The first Program)
? _
(A prompt appears)
(The second Program)
Hi! My name is Barney the Dinosaur! Tell me your name! ? _
(A prompt appears)
(The third Program)
INPUT "Can you tell me your name? ", name$
PRINT "Oooh! "; name$; " is such a lovely name!"
PRINT name$ ", have I told you lately how much I love you?"
And thus you get:
Can you tell me your name? _
(A prompt appears, user enters "Babybop")
Oooh! Babybop is such a lovely name!
Babybop, have I told you lately how much I love you?
Right, so here we are trying to teach you the Input command. You must have noticed by now in the first program
that the command is in fact very simple (and the output too!). All you need to do is to type:
Input variablename$
This will cause a prompt to flash on the screen and a question mark to appear. The characters that you type in will be used to define that variable.
So the syntax basically is:
Input variable-which-you-want-to-define
But surely you want to have something more than a silly question mark there?
(P.S However, please do not pretend to play Batman now. It's lesson time. You are not the Riddler, not the Riddler, you get me?)
So what you do is
illustrated in the second program:
Input "What you want to ask the user for, e.g (Enter your name) "; variable
Remember the ";" sign! Its very important!
So now you know how to ask questions. But. . .how do you get rid of that stupid question mark? Well, have a look:
Input "I want your name: ", name
Note there's a comma instead of a ";". This will get rid of the "?" sign.
- Since you're already here, we might as well teach you how to use the Print command with your stored variables. Its very simple and makes use of the ";" sign too. You just say Print "blahblah"; variable; "blahblah". You got it? Its very simple, remember, just add a ";" after the inverted commas end and after the variable. It's so simple! This will then print the variable as well as the text in the order which you arranged them.
Program Flow

Tired already? Well, this is the last section for the day and a very important one too. Program Flow is a basic fundemental in QBasic. Without Program Flow, QBasic will just be a lousy macro of commands. Program Flow is what makes QBasic so interesting, what makes it jump from one place to another and not just be a stale one-way street, like a french fry. It goes into your mouth and comes out the other way. It doesn't go in the other way and come out of the mouth, or go somewhere into the liver and goodness knows where.
Today we are going to teach you Basic Program Flow. The commands you will learn are: If, Then, and Else If. These are the 3 simplest and most basic commands for control flow, and you are going to learn them!. But first, let's take a closer look at the very concept of Control Flow.
A QBasic program is normally run line by line, from top to bottom as explained earlier. However, with control flow, at certain lines, according to certain conditions, the program, following a control flow command, may switch the way in which it executes the code. Sometimes it skips a few lines, other times it branches to a subroutine. These changes are
activated by conditions.
A condition is usually the state of a variable, e.g. If name$ = "I am Poohbear" Then Goto 1. In this case, the program is told to check the value of the variable name$, and if it is "I am Poohbear", then it is to switch control to line number 1. The condition is IF the value is "I am Poohbear". Only if this condition is satisfied will the program make this move; otherwise it will ignore the line and continue.
Thus control flow is very useful for sifting out and searching for items.
Now how do you use If and Then? Well, as can be seen from above, the syntax is:
If Condition 1 Then Action 1 (Enter) Else If Condition 2 Then Action 2 End If
There is no limit on the number of Else Ifs you can have, its just that we can't type that much out, so we have just included two here. Sometimes only If and Then will be used, sometimes Else If will be used too. If only the former are used, then there is no need for End If. But if the latter is used too, then you must used End If, to signify that you do not want any more Else Ifs.
Now for your assignment!
QB Assignment
Using your knowledge of Control Flow, Directional Input, Variable Subsitution and Color Control, create a QBasic program that displays "I am Barney, I love you" in Screen mode 12 in 15 different colours, which will run for ever if the user does not press Esc to terminate it. You are NOT allowed to manually key in
the color combinations:
e.g. Color 1: Print "I am Barney, I love you"
Color 2: Print "I am Barney, I love you".
Good luck!
PS. Hint: Remember Variable Subsitution? Try a = 1 : Color a + 1. . .?
PPS. Sheesh, that's too much of a hint.
Click here to check your answer!
QB Assignment
Using your knowledge of Input Commands, create a QBasic program that asks the user for his Name, Gender, Age, School, and Telephone Number. Print out the information on the screen in sentences like this:
You are called . . .
You are . . . years old.
Good Luck!
Click here to check your answer!
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]
This Webpage is ThinkQuest entry Unknown.
Email: lccorp1997@hotmail.com
This Webpage is Designed for Netscape Navigator 3.0 and MSIE 3.0
We prefer a 24-bit True Color mode with 640x480 screen mode.
This Webpage created for Thinkquest 1998.