QBasic Tutorial

BY : RUBEN RODRIGUEZ

Chapter 1: The PRINT command... and other various goodies! :-D


I'm writing this assuming you have no previous knowledge in QBasic, or any programming language for that matter... QBasic is a pretty easy language to understand and use. The commands closely resemble english, so the difficulty isn't in reading or writing the actual code... The real challenge lies in making your idea flow logically from one point to the next, without a lot of clutter. But that's how it is in any language, I guess... Anyways, onward!

The PRINT command is one of the simplest, and most common QBasic commands. It is your key to writing text on the screen without a lot of jibba-jabba.

First of all, we should become familiar with the term 'String'. Hello String! A String is, literally a 'string' of characters. "Hello" is a string. "R2F2es" is a string. Strings are stored in memory without the quotes, so "Poeop" is actually just Poeop to the computer. Strings can also be only numbers, such as "1322" but using numbers for strings doesn't have much use for the basic things we're learning here.

Okay. Say you wanted to display "Hello, everybody!" at the beginning of your program. From what we just learned, "Hello, everybody!" is a string. Strings can be directly PRINTed letter for letter. So, let's try it!

PRINT "Hello, everybody!"

I think you guessed that this program would say, "Hello, everybody!" (Without the quotes, of course.) What, you didn't? I give up...

On top of PRINTing strings, you can PRINT numbers, too. For instance, you could write...

PRINT 5

And the output is... (dumdumdum) "5" (minus the quotes, again.) QBasic is also pretty nifty in that you can write:

PRINT 5+3

And like the obedient servant that it is, you'll get an output of "8".

Now let's learn about variables. A variable can be likened to... let's say a folder. Not a computer folder, just a plain old regular folder. You can name the folder anything. (Well, you can't name them after commands like "PRINT$".) The name is purely symbolic, so keep it in context. The case of the variable folder doesn't matter. If you make a variable 'a' then in the next line refer to it as 'A', QBasic will change it to 'A'. You can put things in this folder and close it up until you need to read it, clear it out, or change it. The "papers" that you can put into this folder are numbers, and remember our old friend the string? They can be put into our folder variable as well. For instance...

Num = 67

In this case, "Num" is our variable, and 67 is what we put in the variable. Strings are put in variables in a similar way...

Greeting$ = "Hello, world."

Now there is one fundamental difference. Did you notice the "$" sign? this must always be put at the end for a variable containing a string. If not, you will get a "Type mismatch" error. Mind you, you can put numbers into a string variable by putting quotes around the number, but that is bad practice... The time will come to learn about that, but for now it's off limits, capiché? :)

Variables can be PRINTed just like the numbers and strings we looked at. Watch...

Greeting$ = "Hello"
PRINT Greeting$

The output for this will be... "Hello" (Without quotes, as before) Strings, like numbers can be added to each other, before and while they are being PRINTed. Example:

A$ = "Hello"
B$ = "Dude"
PRINT A$ + " " + B$

The output for this, obviously, is "Hello Dude". What might not be so obvious, though is this thing " ". All this is is a string with a space in it. If this wasn't here, the output would be "HelloDude". Do you understand? I hope...

You can also do it another way, which is more convenient in certain circumstances... Observe...

A$ = "Hello"
B$ = "Dude"
C$ = A$ + " " + B$
PRINT C$

The output is the exact same as the last example. You may ask, "Why the freakin freak did you waste my precious time with that?!" Patience, Grasshopper, I say to you. The time will come for me to reveal greater mysteries. ;)

Ok, one last thing before I go... instead of typing PRINT every time you need to PRINT something, (which will be a lot!) you can just type a '?' and QBasic will automatically change it into a PRINT. (Don't ask me how I know these things... suffice it to say I am God :) Just Kidding.)


Copyright © 2003 Ruben Rodriguez. All rights Reserved.