QBASIC Tutorial

Dale Swanson 11-19-2003

Let me first say that this will be a very, very, basic guide to QBASIC. It is intended as a intro to programming in general, using QBASIC since it's very easy to use. If you plan on programming a lot you should probably learn a better language, like C++ or Perl. However, if you just want to mess around a little, and make some simple text based programs QBASIC has almost no learning curve.

Ah QBASIC, old friend, what a wonderfully simple language it is. If you don't know anything about programing and want to start this is the place to be. QBASIC stands for Quick Beginner's All-purpose Symbolic Instruction Code, don't worry about what that means because it doesn't matter. BASIC was one of the first programing languages around, then Microsoft made a "better version" and tacked a Q on there for good measure. Well that's enough of a background.

So where to start? Well, first thing you need a compiler, which is a program that you use to make other programs. So you can download QBASIC 4.5 here. Download it, unzip it, then click on QB45.exe to begin. It'll probably pop up a full-screen window, don't be scared. If you want to switch back to regular windows to see something without saving and closing your work you can hit the Windows key, which is located between the Alt and Ctrl key and the bottom of the keyboard.

Next you should know your way around, it will take some getting used to because it's a DOS program, so it's just generally weird. Main things you need to know, on the menu, File is where you go to save and load programs, Run is where you go to test programs (start), and make your programs into real exe's, and the help is very, well helpful. It has an index which lists every command in QBASIC, so you can look through them all and click on one to see what it's story is.

Well now it's time to actually start. So the first command you will learn is:
PRINT

PRINT is used to display text on the screen. It's pretty straight forward, use it like this:

PRINT "Hi, world."

That would just put the text Hi, world. on the screen, pretty exciting stuff. Now before I can tell you about the next command I have to tell you about:
variables

Yes, your old friends from Algebra make an appearance in programming, a big appearance. But not to worry, as it isn't very hard to understand at all. You have two types of variable, integer, and string. Integer is numbers, string is words. Any letter or group of letters can be a variable, x, name, amount are all integer variables. But try not use regular words, the thing is if there is a command with that same name you will get an error, so I like to use three letter variable names, tim, for time and such. How do you get a string variable you ask? Add $ after it, so x$, name$, amount$ are all string variables, you can have a integer variable and a string variable with the same name, because the string will always have $ after it. Simple. How do we use variables, well math. You could do the following:

x = 12
y = 5
z = x + y

This would give z a value of 17. You can PRINT variables just by leaving off the quotes. So if we added:

PRINT z

To that last bit it would display the number 12. The math symbols are:
+ addition
- subtraction
* multiplication
/ division
= is equal to
> is greater than
< is less than

There are others but then again there are other types of variables and I didn't tell you about them either. Now although it may seem like witchcraft, you can use the math stuff on string variables:

name$ = "Dale"
last$ = "Swanson"
whole$ = name$ + last$
PRINT whole$

That would print DaleSwanon, with no space, if you wanted a space you could just change the one line to:

whole$ = name$ + " " + last$

That would add in the space in the quote between whatever was stored in name$ and last$.

Next we have:
INPUT

INPUT is used to get info from the user. So if you wanted to ask what their name you would do this:

INPUT "Hi what's your name"; name$

That would print hi what's your name and then would display a question mark. The program would wait and the user would have to type something in and press enter to continue.

Now back to PRINT for a second, because there is one thing I didn't tell you about and that is this:

PRINT "Hi "; name$; " good to meet you."

Let's assume they entered their name as Dale. This would display Hi Dale good to meet you. Note that there is a space after Hi and before good.

So now you have a pretty good understanding of how things work. Now it's time to tell you about the second biggest thing to programing after variables:
IF...THEN Statements

These are kind of complicated, but not really. IF...THEN statements are how you let the program make choices. Really the best way to explain is through example so:

INPUT "Enter a number 1-3"; num
IF num = 1 THEN PRINT "One is a good number"
IF num = 2 THEN PRINT "Two is ok"
IF num = 3 THEN PRINT "I hate three"
IF num > 3 THEN PRINT "You're dumb"

Got it? I sure as hell hope so, cause I'm about to lay down the second layer of it:
Block IF's and ELSE

Block IF's and ELSE are an add on to IF...THEN, again the best way to explain is through example so:

INPUT "Enter a number 1-3"; num
IF num = 1 THEN
 PRINT "One is good"
 PRINT "It is the best choice"
ELSEIF num = 2 THEN
 PRINT "Yeah, two is ok"
 PRINT "I guess"
 PRINT "But one is way better"
ELSEIF num = 3 THEN
 PRINT "Three is terrible"
 PRINT "It is the worst one"
ELSE
 PRINT "you are dumb"
 PRINT "how hard is it to pick a number between 1 and 3"
END IF

Ok, so you should be able to figure out what is going on there, but let me go over it. You input the number, if it's 1 it does everything under that THEN up until the ELSEIF, if it wasn't 1 then it will go to the next ELSEIF, it will only go to the ELSEIF if it has not found an IF that was true yet. So:

x = 1
IF x = 1 THEN
 PRINT "whatever"
ELSEIF x = 2 THEN
 PRINT "this cant happen"
ELSEIF x <; 10 THEN
 PRINT "this wont happen"
ELSE
 PRINT "this wont either"
END IF

What will happen here is whatever will be printed, and thats it. Even though x is less than 10 it won't do it because it already found a true IF. That basicly it, but here are some extras about block IF's. You have to indent, you see how all those PRINTs are one space in? Well you need to do that, by doing thing it will keep things from getting crazy. You will be putting huge amounts of code after the IF's, and you will even be putting other block IF's in the block IF's. So you have to indent each layer of IF one space. Also notice how I ended the IF with END IF, you don't need to do that with a simple IF...THEN, but if it spans more than one line you do.
One more thing about IF...THEN's:

IF x$ <> "fun" THEN PRINT "whatever"

So what does this do? It says if the string contained in x$ does not equal fun then print whatever. If you just use <, or > with a string it will give you an error because a string can't be greater than or less than, but if you want to say not equal to that's the way.

So you can now make some simple programs. But for games you really will always need chance or random numbers:
RANDOMIZE and RND

If you are going to be using random numbers in your program (and you almost always will be) you need to put RANDOMIZE TIMER at the top of your program, this is very important. If you don't the program will still run, and you will still get numbers that seem random, but they will be the same every time you run the program. This is because a computer can never produce truly random numbers, only pseudo random numbers. The computer uses a complex formula to come up with random seeming numbers but it will produce the same set every time. We fix this by seeding the random number generator (RNG). It uses the seed number as a base in the formula to produce a different set of random seeming numbers. The same seed number will produce the same set of numbers, which can actually be useful for testing. The command RANDOMIZE TIMER is using the system variable TIMER to seed the RNG. TIMER is the number of seconds passed since midnight, and thus it changes every second. You could use any other variable or number to seed, but TIMER works great since it's always changing. So how do you get the random numbers? You need this little crazy formula:

x = INT(RND * max) + min

That will give a random value to x, of any number from the min to the max. You would replace max with whatever the max number possible you want is, and your replace min with the minimum (usually left as 1). If you wanted a random number from 50 to 100 including 50 and 100 as possibilities then you would use this:

x = INT(RND * 100) + 50

There you have it, the hardest part is remembering that crazy formula. The help file in QBASIC under RANDOMIZE has an example though.

So now for something that is very useful:
GOTO

GOTO has to be used with line labels, it's a pretty simple but good idea:

start:
INPUT "Enter a number 1-3"; num
IF num = 1 THEN
 PRINT "One is good"
 PRINT "It is the best choice"
ELSEIF num = 2 THEN
 PRINT "Yeah, two is ok"
 PRINT "I guess"
 PRINT "But one is way better"
ELSEIF num = 3 THEN
 PRINT "Three is terrible"
 PRINT "It is the worst one"
ELSE
 PRINT "you are dumb"
 PRINT "how hard is it to pick a number between 1 and 3"
 GOTO start
END IF

The same number picking program from above, except now, if the user doesn't pick a number from 1 to 3 it goes back to the start and they have to do it again. The line label can be anything, and you can have as many as you want where every you want, just put a word then a colon, and there is a line label. Also the same thing that applied to the variable names applies here, the name can't be the same as something else, note that this includes variables, you can't have a label and variable with the same name. There's not really much more to say about GOTO, but it will be used a lot.

Note most programmers consider GOTO to be Satan himself. The reason is it makes your code harder to follow, which makes it harder for someone else or you to look over your code at a later date and change it. If you are only going to be making simple stupid programs for fun you can pretty much ignore this. However, if you plan to program a lot you should probably get in the habit of using GOSUB instead. GOSUB is pretty similar, except it only go to the place temporarily, and returns after it does whatever is there to be done. You can look up GOSUB in the help file for more details.

Now for something that is very simple:
CLS

CLS is CLear Screen. It clears off all the text that is on the screen so far, making it look better. For example, in the number picking program CLS would be added right after start:, that would clear the screen every time they go through the program.

Here is an useful and easy one:
END

I'll let you guess what this does. If you just let the program run out of code it ends the program, but this is useful if you want to make a do you want to play again type thing.

One simple thing is:
'

That's right a single quote, a ' that's all. This is the comment command. You can put it anywhere and anything after it will be ignored:

'PRINT "You won't see this"
PRINT "you will see this" ' but what ever i put here don't matter at all weeeeeeeeeee print input fun

In that bit of code all that would happen is you will see this would be displayed, PRINT "You won't see this" will be ignored, in fact even though there is another ' in it (won't) it won't turn off the comment or anything. And in the next line, all that random crap after the ' won't happen either. "Good" programmers use ' a lot, they use it so they can go back to their work latter and understand what is going on fast, and so that other programmers can look at it and understand it. I rarely use it, in fact the only time I'll ever use it is if I want to make a section of code not run, but I don't want to delete it. I'll just put ' in front of each line. Well that's it.

Well, as of now you know everything there is to know about QBASIC, you will be able to make some of the finest programs on Earth. Ok, well not really, but you know enough to make some programs. You can make some pretty good (and by good I mean shitty) games. For example here is some piece of crap, that picks a number from 1 to 100 and then you try to guess it, each time it tells you if you are too high or too low:

CLS
RANDOMIZE TIMER

PRINT "Hi welcome to the number guessing game!"
INPUT "What's your name"; name$
PRINT "Hi "; name$

start:
CLS
x = INT(RND * 100) + 1

guess:
turn = turn + 1
INPUT "Ok, pick my number from 1 to 100"; g
IF g = x THEN
 PRINT "You win, it only took "; turn; " turns!"
 turn = 0
 GOTO ask
ELSEIF g > x THEN
 PRINT "My number is less than "; g
 GOTO guess
ELSEIF g < x THEN
 PRINT "My number is more than "; g
 GOTO guess
ELSE
 PRINT "I don't know how this happened" 'it should be impossible to get here so i just put this
 END  'if they got here somehow then something is wrong so I just ended the program
END IF

ask:
INPUT "Do you want to play again, y or n"; a$
IF a$ = "y" THEN
 GOTO start
ELSEIF a$ = "n" THEN
 CLS
 PRINT "Ok, bye!"
 END
ELSE
 PRINT "You need to choose y or n, lower case"
 GOTO ask
END IF

You should look over that, feel free to use it, and add more stuff. Let me say a few things, notice the line turn = 0, this is a common mistake for new programmers, if you left that line out it would just keep adding up turns. So it would be saying stuff like it only took you 16 turns, then 24 and so on. Also notice how stupid and happy the program is, I don't know why I made it like that. Let me say one more thing, when you save your work in QBASIC you save it as .bas file, well you will notice a option to save it in either QuickBASIC or text, if you save it as text you can open it in notepad. Having your code in notepad makes it easy to copy and paste it to elsewhere. So if you want to you can take that code up there, copy and paste it into a text file, save as program.bas and then you will be able to open it in QBASIC, and mess around with it.

Ok so now after looking over that one I think it's time to give you some homework. This is what I want you to do, make a rock, paper, scissors game. If you don't know what rock, paper, scissors is search for it, but here is the basic summary. Two players each pick rock, paper, or scissor, then compare what they have, rock beats scissor, scissor beats paper, and paper somehow beats rock. It's pretty simple and if you don't already know how to play it there is something wrong with you. Anyway this is the first program I ever made. I don't still have the code to the first one I made, but I will write something up and then put it here, so you can compare what I did to what you did. Remember the goal of any program is to get the job done in the least amount of lines, more lines equal more size, longer downloads, longer to start the program. But, don't let that stop you from putting in funny stuff, this is a game remember, it doesn't have to get the goal done in 100% least amount of lines. Just don't waste lines doing things the wrong way.

Ok, make the program, I'm going to make mine, when you get it to work (or if somehow you get stumped) come back and compare what I did to what you did.
GO!

Ok, so here is my version:

CLS
RANDOMIZE TIMER

start:
CLS
cpu = INT(RND * 3) + 1
PRINT "1-Rock, 2-Paper, 3-Scissors"
INPUT "Rock, Paper, Scissors, shoot!"; rps
IF cpu = 1 THEN cpu$ = "Rock"
IF cpu = 2 THEN cpu$ = "Paper"
IF cpu = 3 THEN cpu$ = "Scissors"
IF rps = 1 THEN rps$ = "Rock"
IF rps = 2 THEN rps$ = "Paper"
IF rps = 3 THEN rps$ = "Scissors"
PRINT "Me: "; cpu$
PRINT "You: "; rps$

IF cpu = rps THEN GOTO tie

cpu = cpu + 1
IF cpu > 3 THEN cpu = 1
IF rps = cpu THEN GOTO win

l = l + 1
PRINT "I win!"
PRINT "I have won "; l; " games, and you have won "; w; " games!"
INPUT "Do you want to play again 1-yes, 2-no"; p
IF p = 2 THEN END
GOTO start

tie:
PRINT "Tie!"
INPUT "Do you want to play again 1-yes, 2-no"; p
IF p = 2 THEN END
GOTO start

win:
w = w + 1
PRINT "You win!"
PRINT "I have won "; l; " games, and you have won "; w; " games!"
INPUT "Do you want to play again 1-yes, 2-no"; p
IF p = 2 THEN END
GOTO start

Well, it was kind of hard to figure out the whole compare the computer and player and see who won thing. And there is really no simple compact way to do it, but I'm pretty sure that's the smallest amount of code that can get the job done.

So how did you do? This test will really show if you have what it takes to become a programmer. If you couldn't think of any way to make it work you just don't have what it takes, go kill yourself now. Anyway back to you nonlosers, good job, now you can start the process of adding new commands to your programs. You can make programs with what you got now, but here I will list some helpful extras:
AND OR

These are more stuff for IF...THEN's, here is an example:

x = 1
IF x = 1 AND x = 2 THEN PRINT "whatever"
IF x = 1 OR x = 2 THEN PRINT "whatever"

Ok so in this case the AND won't happen, but the OR will. Now AND and OR's can get very very very confusing. Many programmers that are far better than me or you ever will be have messed up with them. Take this example:

CLS
IF 1 = 2 AND 1 = 2 OR 1 = 1 AND 1 = 2 OR 1 = 2 OR 1 = 2 AND 1 = 1 AND 1 = 1 AND 1 = 1 THEN PRINT "fun"

What the hell would happen here? Well to make it easier to figure out I've used numbers instead of variables. The way it works like this is to just look at the OR's, and split them up into there own IF's in your head. It's pretty crazy, and you can either just forget about them and don't use that many OR's and AND's or just open QBASIC and figure it out through trial and error.

Next up: BEEP

This makes a little beep through the internal speakers, it may be hard to hear.

Well that's all I feel like doing now, and I will probably never finish it, but you should be about to figure the rest out on your own at this point. Here are some other good things you should look up in the Help file:

DO...LOOP - Used to do things over and over.
FOR...NEXT - Used to do things a certain number of times, while changing a variable with each pass.
GOSUB - Like GOTO, only it returns to where it left.
INKEY$ - A system variable that contains the currently pressed key.
LCASE$ - Makes a string lower case.
LEFT$ - Gives a certain number of characters from the left of a string.
LEN - Gives the length of a variable.
SOUND - Makes a sound, great to annoy the user.
SLEEP - Pauses the program for some seconds.
STR$ - Gives the string version of a number.
TIME$ - System variable that contains the current time, as a 24 hour clock (18:03:24 is 6pm 3 minutes, and 24 seconds).
UCASE$ - Makes a string upper case.
VAL - Gives the number form of a string.

Well, that's all, now get the hell out of my yard, damn kids.

Home

Stuff I Wrote