Choose Which Part Of This Page You Wish To Go

[Introduction To 1st Program] [Input with Variables] [Assigning Variabels and IF, THEN, and other commands] [Loops] [GOTO command] [Adding Sound] [Incorporating Graphics]


The basis of almost all programming languages is QBASIC because it contains familiar commands that the others do. So once you master QBASIC, you'll soon find other languages much easier. You might already have QBASIC on your computer, but if you don't, just run a search on the web for QBASIC and you'll be sure to find it free. Unlike the C tutorial which is going to really go into detail, this tutorial should be able to be read in at least a few days to a week. You won't learn all of QBASIC, but just enough where you can write address books and records and things like that. Okay? If you want to learn the entire language where you can write games and things like that, you'll have to check out another site. Sorry, I wish I could tell you everything about QBASIC, but there is so much to unlock. Well, you're probably anxious to see an example program, so here is a simple one that pretty much does practically nada thing. CLS COLOR 12 PRINT "I'm light red text!" COLOR 14 PRINT "I'm yellow text!" That was it. Not a very exciting program or anything, but it's a good start. That first command, CLS told the computer, "Just clear the screen!" Nothin' scientifically boggling to the brain or anything, just your run of the mill command in QBASIC.

Now as you move on to the second line you'll see the word COLOR. The great thing about QBASIC is that it has a lot of words that are quite obvious to understand what they're doing to the program. That color command told the computer to make the text light red. Below are the 16 colors QBASIC uses -

If you want to change the text to a certain color, you just type in COLOR xx (xx being whatever two digits the color is). Simple, huh?

Now on to something just as simple as the last command. It's PRINT. All this does is print on the screen whatever is inside of the quotes following it. So if I wanted to print the word NRRRRD on the screen, all I would do is type in PRINT "NRRRRD"

I added another COLOR command so when I typed in a new color, it just changed the text color from light red to yellow. Unlike HTML, QBASIC doesn't need any anchoring tags. And the rest of the program is pretty much just repeating the PRINT command.

Now you're probably thinking that was a pretty pathetic program (I majorly agree with you), but it's something simple that will help you with the next section - processing information that the user provides.


What makes programs so great is that they can revolve around the users input. So let's say you're going to do a very simple verification program. Why that can be done in four lines! It will also introduce you to using variables.

CLS INPUT "What operating system are you using"; os$ PRINT "Oh, so you're a "; os$; "user." END The first line contained the command CLS. That told the computer to clear the screen. Pretty simple. Then, moving on to the second line, you'll come across INPUT. That told the computer it should allow the user to type something in. Then on the input line, it asks a question that can be answered by the user, in this case, the question is what operating system are you using? Right after the question, a semicolon follows (you can put a comma in, instead of a semi colon, but for now, just use a semi colon) and then next is a variable that can be whatever you want it to be. Make sure to include the $ sign after the variable you choose for this input command because it specifies that the variable is a string.

On the third line, you'll notice the familiar print command. But this time it contains the string variable os$ that I specified in the first line. Here's what's going on - the computer asked the question, put the answer in the variable string and so then in the second line, the semicolon breaks off the beginning part of the sentence, inserts whatever answer the user had specified, and then blocked it off with another semicolon and ended the sentence with the word user. So let's assume the user replied to the question that he or she used Windows. The computer would have saved the string Windows in the variable and so when the output ocurred, it would have said: Oh, so you're a Windows user. And that's it.

Now for practice, I want you to try and write a program that asks the user for a password, have them type it once more, and then verify it by printing it on the screen. Be sure to use the same variable on both input lines when prompting them for a password. Then test it by typing in a different password on both lines. I'll leave you to find out what QBASIC does.

Assigning Variables

Okay, now we're going to jump into assigning variables. On the above explanations, you worked with a string variable, so let's concentrate a little bit on strings for now so you can be sure that you understand it. Only use strings when you're doing variables that work with text and not numbers. Here's an example -

CLS INPUT "Enter in your name please: ", name$ PRINT "Why hello"; name$ END And that's it. It's got the $ sign after the variable name to signal that it's a string so that the computer should be ready to use text as the variables value. A person wouldn't type in 3744949 as their name, but if they did QBASIC would have them automatically retype in their name - letters only.

The next type of variable we're going to learn about are integers. Integers are made up of one number. Here's an example of how integers are used.

CLS INPUT "Press 1 if you would like to see a hello message.", number% IF number% = 1 THEN PRINT "Hello" ELSE PRINT "Okay, guess you don't want me to say hello" END I know, I added in some new commands like IF, THEN PRINT, and ELSE PRINT. But don't get all freaked out, here's what's going on. The first line is just like the other examples, only it is a integer variable. Then on the second line, the IF thing just means, IF the integer variable = 1, THEN PRINT the word Hello on the screen. If it equals something ELSE, PRINT 'Okay, I guess you don't want me to say hello' on the screen. The END thing is optional, but it's usually a must in some versions of QBASIC when using those statements. When writing programs, just use END, it will make it so much more easier.

Now that you have a good idea of strings, you're going to learn the other variables. Next on our list are integers. Integers are negative or positive whole numbers. Integers are referred to as integral numbers and you'll see that description used often on the net.
When doing calculations with integers, the speed of the program goes much faster. Here's an example of a program using integers (and did you know that you used integers in the above program - the integer was the variable with the % following it).

CLS INPUT "What's your age"; age% PRINT "That's pretty cool that you're"; age% ;"years old." END Type in that code and then run it. Now, instead of typing in your real age, test it out by typing in one or more letters. You'll find that QBASIC gives an error saying Redo from start and then asks the question again. That's because the variable wasn't a string so the program wasn't ready to accept text.

Okay, so now I'm going to sum up the long, single, and double variables. Long variables are usually no decimal. Single are smaller numbers with a longer decimal place and double are larger numbers with smaller decimal places. For example, when using 3.1415 (pi), you would express it's variable as a single variable. You'll be able to understand this more as we go along.

Now that you understand variables in QBASIC, you'll need to know what symbol stands for what.

You should really memorize that. It'll come in real handy. Remember that the symbol for the variable always comes after the variable. So if you have a string variable named john, then the variable would be john$. Got it?


Well, you've made it this far so it looks like you're ready for loops. Looping comes in really handy because let's say that you write a program for your algebra homework and you have to do 20 problems. Then it wouldn't make any sense to just have to re-run the program over and over again. That's where the LOOP command comes in. LOOP always begins with DO and ends with the statement LOOP. You can set how many times a LOOP goes, but I think that manually ending a LOOP is always best, although sometimes it isn't. Here is an example of a loop (but not a useful one) CLS DO 'begin loop INPUT "What is your age (press 999 to exit): ", age$ IF age% = 999 THEN EXIT DO PRINT "That's pretty cool that you're"; age%; "!" LOOP END Now, I have to admit, that was not a useful LOOP command, but if you wanna get on someone's nerves majorly, that's the program to use, that is, if you take out a few things and make it look like this: CLS DO 'begin loop INPUT "What is your age: ", age$ PRINT "That's pretty cool that you're"; age%; "!" LOOP END Then it will go on forever. There are other ways to use the loop command, such as if you've made a program like I mentioned above that will let you compute mathematical answers.

Let your imagination go crazy with the ways you can use loop. But before we move on, note that we're not finished with loops. Here's some more examples.

FOR s = 1 TO 1000 PRINT s NEXT s This program is fairly easy to explain. FOR the variable s, let it equal 1 going TO 1000. PRINT on the screen, the variable s starting from the beginning, moving on to the NEXT number in the variable s.

GOTO command

Okay, here's another type of loop. start: CLS PRINT "This is an example of a loop. The screen will be cleared, and then this text will play over and over again" GOTO start On the first line, it's labled start. It could be labled anything, but I just chose start. Then comes CLR so it clears the screen so that every time it loops, all that stuff will be cleared. Next comes the print command so nothing special. But on the third line is when it gets semi-interesting. GOTO start. This just means what it says. Go to start or whatever you named it. You can also name it a number like 1 or 2 or whatever and then say GOTO 1.

Well, you've survived most of QBASIC so now we're on to more complex things. The next sections many involve a bit of looping, lessons more on variables and mathematical equations. So let's say that you're younger brother or sister or cousin or whatever is learning how to square whole numbers in math class, but they want you to explain it. What's a NRRRRD to do? Well, you can always write a program that does it for them and explains everything as they go along so they also get a good knowledge of how to do it. Here's an example. Note that this program is only for smaller whole numbers and not really long numbers. But you can always change that giving them a option like asking whether it will be a decimal or small whole number or a long number, and so on and then you can send them to that part of the program. For now, we'll concentrate on something easy.

start: CLS INPUT "What is the whole number that you need to square"; number% answer% = number% * number% PRINT "The answer is", answer% PRINT "To square"; number%; "I took,"; number%; "*"; number% END Of course, if you want to make the program work with all types of numbers, you can ask the user what type of number, give him or her available choices, then use the goto command. But that's all up to you.

Adding Sounds

The next thing we're going to learn in QBASIC will be on how to add sounds to your program. Oooh . . . Ahhhh . . . I'd say so. So let's get started. CLS REM this sound program was written by a teenage NRRRRD SOUND 500, 25 END Type that in and you'll hear a somewhat long beep. That 25 determined how long the beep was. I'd say it lasted about 7 or 8 seconds. And that 500 determined the pitch. The higher the number, the more higher the pitch. Go crazy, annoy your online friends by sending them the program and make it loop. And if you want to add music then just put a bunch of the sound codes together (each sound command on a different like though). Did you notice that I also added REM? I figured that I talk enough about commenting in my other tutorials, why not in QBASIC also? QBASIC will just skip over that line and ignore it so it is just a comment that is only visable in your source code. Anyways . . .


Ready to start making some graphics in QBASIC? Believe me, it isn't all that hard. It will add a nice addition to your programs. Here is an example: REM this program displays a filled in circle CLS SCREEN 12 CIRCLE (200, 250), 100, 14 PAINT (200, 250), 14 Usually people will tell you to use SCREEN 13 because of it's capabilities. Since there are 13 SCREEN modes, as you progress from 0 to 13, they accumulate in their capabilities. But through your QBASIC programming, you might only use 3 of them, SCREEN 0 (which is text mode), SCREEN 12 (which is 480x640 with 16 colors), and SCREEN 13 (which is 320x200 with 256 colors). But for these examples, I will use SCREEN 12. You have to specify a screen so QBASIC knows what to use for your monitor et cetera. Just for now, know that you should use SCREEN 12. Then came the command CIRCLE. This just told QBASIC that it was going to be a CIRCLE. Inside of the paranthesis were what specified the position, in a matter of speaking the column and row. Note that you always have to specify a radius and that's what the 50 was. The radius determines the size of your circle. The bigger the radius, the bigger the circle. Now so far, you have specified the circle's size and it's border color (the 14 is the border color which is the value for yellow) but what if you want your circle to be fully colored in? Then you can just use the PAINT command. PAINT fills in your circle, but you should use the same coordinates of your circle if you want the circle filled in that is. So I listed 200 and 250. And I decided I wanted my circle to have the same color as the border so I used 14. So all this did was set a circle on the screen. But what if you wanted to make a lot of circles appear on the screen? Well you could make it loop. More on this at the next update. For now just play around with making circles.

But if all you used were circles, then your programs would have no point. You should learn how to plot points with the PSET command. We will do this in SCREEN 13.

SCREEN 13 PSET (75, 45), 04 END That will plot a point at the coordinates of 75 and 45. And the point will be in dark red (that's where the 04 comes in, although QBASIC may change the 04 to plain 4 for efficiency).

You can also make lines in QBASIC and save yourself time with the pset command by doing this:

REM this program displays a line SCREEN 13 LINE (10, 50)-(1500, 50), 4 The line will begin at the 10, 50 coordinates and end at the 1500, 50 coordinates. The dash (-) is important! This way QBASIC knows that the two sets of coordinates are connected. And if you want to make your line colorized, add a color value at the end like I did (dark red, value 4).

Update soon to come!