QBASIC Tutorial
Last Updated: 3/14/04
© Mike Detwiler 2004
TABLE OF CONTENTS
===================================
I. Introduction
II. QBASIC Tutorial
1. Basic Output/Misc.
1.1 CLS
1.2 PRINT
1.3 Comments (')
2. Variables
2.1 Interger (%)
2.2 String ($)
3. Program Flow
3.1 INPUT
3.2 IF...THEN...ELSE statements
3.3 DO...LOOP loops
III. Challenge Answers
IV. Confused?
====================================
_____________________
/ I. Introduction /
-----------------------
You're reading this tutorial probably never having programmed anything before in your life,
but I will try to make this a great starting point. QBASIC is a good first language to learn,
and while you might not be making 3D first person shooters with it, you will gain some of that
foundation if you ever do become a serious programmer. After each section, I've included
challenges to do involving each command. You don't have to do complete them, but it is
recommended before moving on to the next section. Also, run the examples if you're having
trouble.
_________________________
/ II. QBASIC Tutorial /
--------------------------
____________________________
/1. Basic Input/Output/Misc. \
/ \____________________________________________________________
1.1 CLS
*************************************************
There isn't much to explain here. CLS simply clears the screen of any text or graphics
(the one that pops up when you go to Run>Start (or press F5).
Wherever you insert CLS, the screen is cleared. This command is important at the beginning of
every program to clear the screen of any graphics left over from the last time you ran it.
No challenge here, just get in the habit of starting each program with CLS.
1.2 PRINT
**************************************************
PRINTs the text you type to the screen in between the "" marks after print.
Example:
================
CLS
PRINT "Hello World"
================
Output:
================
Hello World
================
Pretty simple, eh? Well, for the most part, it is. Just remember opening and closing "" marks
or else you will get an error.
Challenges: (remember to start the program with CLS!)
Make a seperate program for each line that displays the following text:
1]
==========
I am a robot.
==========
2]
==========
00000
0000
000
00
0
==========
1.3 Comments
*****************************************************
A comment is anything that you want to add as a note to yourself without the computer trying
to read it. If you just type in random stuff without a comment marker, you will get errors.
Mark a comment with a ' mark.
Example:
===================================
PRINT "Blah blah blah" 'important step!
===================================
This way, the comptuer will see the ' and stop reading. So, it will ignore where you wrote
"important step!"
NOTE: Comments can also be expressed with REM --
Ex.
========================================
PRINT "Blah blah blah" REM important step!
========================================
-- But it's much neater if the ' is used instead.
______________________
/2. Variables \
/ \______________________________________________________________
A variable is a value like a number or a string of letters that changes. For programs to have
any variation at all, some values have to change. The early programs you write will have very
few variables, but consider an RPG. There must be variables, or numbers, that change to govern
almost every aspect of the game, from HP, Mana, what spells are in a player's inventory, gold
on hand, etc. The following is some general information about variables in QBASIC.
To declare (make) a new variable, simply insert a line of code with the variable name, and set
it equal to a value.
Example:
======
X = 0
======
In this case, the variable, X, is equal to 0. But, variables do not have to be only one number.
Example:
=============
somenumber = 15
=============
Here, the variable 'somenumber' is equal to 15.
You can also change variables whenever you want with the four basic operations (+, -, *, /),
raise the variable to an exponent (^2 after a variable would raise it to the 2nd power), or
perform other operations which are listed in the QBASIC help file.
Examples:
X = X + 1
Y = Y - 20
agreatnumber = (agreatnumber + 4) * 20
You've just learned about variables with no special symbol, but here are some examples of
two other often used kinds.
2.1 Interger (%)
*****************************************************
Example:
==========
Y% = 15
==========
The variable Y% = 15. If the value is ever changed, then Y% will round it to the nearest
whole number if necessary. So, if for some reason Y% = 21.921, then Y% will = 22.
2.2 String ($)
*****************************************************
Example:
==================
name$ = "Mike Detwiler"
==================
There's my name, stored in a string variable. This comes in handy if you want to PRINT
certain text that could change, depeding on the user's actions.
To set a PRINT command to print from a string variable:
Example:
==========================
CLS
name$ = "Mike Detwiler"
PRINT name$
==========================
Output:
============
Mike Detwiler
============
Note how in the above example, in the PRINT statement, name$ has no "" marks around it.
This tells QBASIC that name$ is a variable and not text to be printed to the screen.
So QBASIC looks for the variable name$, and PRINTs what it contains (in this case, Mike Detwiler).
______________________
/3. Program Flow \
/ \________________________________________________________________
3.1 INPUT
***************************************************
INPUT is the opposite of PRINT. Instead of you, the programmer, telling the computer
what to put on the screen, the user puts on the screen what the program must remember.
The syntax for INPUT:
INPUT "any text you want here, like a question, or nothing", [variablename][type, if applicable]
So, an example would be:
INPUT "What is your name? ", name$
This example would store whatever the user enters in the string variable name$. INPUT
and PRINT are often used together, like in this next example:
============================================
CLS
age% = 0
INPUT "How old are you, in years? ", age%
PRINT
PRINT "You are "; age%; " years old."
=============================================
What each line in the Example does:
CLS
Clears the screen.
age% = 0
Declares the variable age%, and sets it equal to 0.
I made this a % (interger) variable in case the user types something like 3.5.
That way, it will be rounded to 4.
INPUT "How old are you, in years? ", age%
This prompts the user for their age, and stores it in the variable we've already made, age%
PRINT
This prints a blank line, for neatness. PRINT "" would be another way to do this.
PRINT "You are "; age%; " years old."
This inserts the age% that the user typed in into a PRINT statement. To insert any variable
into the middle of a PRINT statement in the future, use the syntax shown here.
Challenges:
1] Make a program that asks the user for the prices of 2 objects. Add the two variables
together, and then give them their total.
3.2 IF...THEN...ELSE statements
************************************************
IF you are learning Qbasic THEN
you are a nerd
ELSE you are very confused
Above is a basic example (no pun intended) of what an IF...THEN statement is. The proper
syntax is
IF [condition is true] THEN
[do stuff]
*ELSE
[then do this instead]
END IF
*The ELSE line is optional, but usually handy.
IF [condition is true] THEN [do stuff]
all in one line is also acceptable, then you won't have to use the END IF statement.
Ex.
======================
CLS
X = 2
IF X = 3 THEN
PRINT "X equals 3"
ELSE
PRINT "X doesn't equal 3"
END IF
======================
This program checks if the value of the variable X is equal to 3. Since we set X equal to 2,
and included an ELSE statement in case X didn't equal 3, then the program will print "X doesn't
equal 3". And please -- don't forget the END IF line, doing so will cause so many weird errors
later on.
3.3 DO...LOOP loops
***************************************************
Even if you've never programmed, you can probably figure out what a loop is -- a section of
code that will repeat after its completed repeat after its completed repeat after its completed,
and then ends when a certain condition is met. DO...LOOP loops DO the stuff after the DO line
until it reaches the LOOP line, then it will go back to the DO line, and do it again...and again,
and again.
Ex.
=========================
CLS
X = 1
DO
PRINT X
X = X + 1
LOOP
=========================
This program sets the value of the variable X to 1, then prints the variable X, adds one to the
value, and then repeats it forever. What happens is your computer display should count up in
increments of 1. Not very useful, but when combined with IF...THEN statements, these loops are
very useful.
Now, this program will never end. Say you want the computer to count to 100, and then stop.
Simply change the program a bit:
=========================
CLS
X = 1
DO
PRINT X
X = X + 1
LOOP UNTIL X = 101
=========================
Now, the computer will actually count to 101, but it will only print numbers up until 100.
Challenges:
1] To make a guessing game where the user must guess a number from 1 to 10. The if user is too
high, then tell them to guess lower. If they're too low, tell the user to guess higher. When the
guess correctly, make something special happen.
____________________________
/ III. Challenge Answers /
-----------------------------
1.2 PRINT
++++++++
Challenge 1]
CLS
PRINT "I am a robot."
Challenge 2]
CLS
PRINT "00000"
PRINT "0000"
PRINT "000"
PRINT "00"
PRINT "0"
2.5 INPUT
++++++++
Challenge 1]
CLS
item1 = 0 'like i said, give youself a list of variables at the beginning or else as
item2 = 0 'your programs get bigger and bigger you will loose track.
total = 0
INPUT "Cost of item 1:", item1
INPUT "Cost of item 2:", item2
total = item1 + item2
PRINT "Your total is"; total
2.7 DO...LOOP loops
+++++++++++++++++
Challenge 1]
Remember to fully utilize the all of the commands you have learned. Your program should
first declare what you want the secret number to be (for example, X = 4), then have one main
loop that asks the user for a guess, checks to see if its too high, too low, or just right,
then the program should respond correctly.
_____________________
/ IV. Confused? /
-----------------------
Programming in any language can be very frustrating at first. If you're totally stuck
and have no idea how you would go about programming something, drop me an email at
Mike0101@att.net. Make the subject line something appropriate and I'll be sure to
look at it. Once I get some questions, depending on what they are, they may or may
not be posted in this tutorial to help others.
END OF DOCUMENT
Home