Microsoft QBasic
Click here for site map

Home
Intro
Lessons
Library
Reference
About
www.microsoft.com

This is a line. . .a short one

Lesson 1

This is a line. -------- If you don't know what a line is, you're brain dead.

What you will learn in this lesson
  • Basic Text Commands: Print, Cls, Locate
  • Basic Color Commands: Screen, Color
  • Basic Programming Tips: ":", (Line Labeling) 1-2
  • Basic Misc. Commands: Sleep, End, Shell, System

Lesson 1

This is a line. -------- If you don't know what a line is, you're brain dead.

Welcome to your first day at school! Today we are going to teach you the basics of QBasic. And what better basic to teach you than text? Text is basically the foundation of QBasic, and it is pretty logical that you, the initiate, should start off with Text Commands! To do this, let's show you a code sample.


Basic Text Commands

Type this into your copy of QBasic, and press F5 to run it.

Raw Code

SCREEN 12: COLOR 15: CLS
PRINT "Hello World, its your birthday today!
PRINT "World: No, my birthday is long gone."
PRINT "Don't be a fool!": PRINT " Your Birthday is on the 1st of April!"
LOCATE 5, 1: PRINT "World: Yes, but I was born 500 million years ago!"
END

Program Output

Hello World, its your birthday today!
World: No, my birthday is long gone.
Don't be a fool! Your birthday is on the 1st of April!

Yes, but I was born 500 million years ago!

Dissection

What did you see in that code? I'm sure you noticed the Print command. And what does it do? It prints out text on the screen! Note the simplicity of the command, the beauty of it, the glorious aura of radiance that eminates from it. All it takes is one simple word, and this whole line of characters appear! Magic, isn't it? Wonderful! Anyway, I'm also sure that you noticed that we highlight commands/keywords like this. Print. This is to help you understand code better. QB does that too. Now lets go through the code line by line, so that we can explain each command and concept to you clearly.

Line 1
Screen 12 - This sets the screen mode to VGA format with up to 256K colors to 16 attributes.
Color 15 - In Screen 12, Color 15 is the color attribute for white. Thus the text will be white. Cls - Cls is the command to clear the screen of unwanted material.

Cls is important!

Note Please note: It is always advisable to have a Cls before the start of any program. Thus you can be assured of a fresh screen and that your program won't show any leftovers from the previous one.

Line 2
Print - From this line you can see how the Print command works. The syntax is basically Print "text you want".

Line 3
Print - This line clearly shows how to leave blanks in a line. Note that the 2nd line is not filled and that the 3rd line's output starts on a fresh line. Thus every Print "" is equivalent to 1 line of output.

Line 4
Why did we put a semicolon (:) in the middle? No, it wasn't for fun. This is to show that a semicolon joins lines of code together, acting on them as one unit. Thus Print "text1": Print "text2" is different from Print "text1" (Enter) Print "text2" in that the output will be displayed on a single line:

Text1Text2

Instead of:

Text1
Text2

Line 5
Here you can see how the Locate command works in conjunction with the semicolon. The syntax for the Locate command is Locate row, column. Thus Locate 2, 5: Print "text1" will give you:

    Text1
Note that in the code above, the beginning of Text1 is 5 spaces off. Also note that in the example code, a gap is left in between Line 3 and 5 of the output. This is to show the gap caused between the end of Print (3rd line) and the start of Locate command (5th line). Well, that's all for the code. Quite a lengthy explanation for 5 simple lines of code, but we believe in doing stuff thoroughly.

Back to Top


Basic Color Commands

Screen and Color

The two color commands in question here are the Screen and Color commands. As the Screen command has already been described in the section above, we shall concentrate on the syntax of the commands instead. Here it is, all dished out on a plate for you. I hope you remember your elementary school teacher; she used to say, "You're not babies, lets' not spoon-feed you anymore!"

Screen x (a number from 1-15)
Color y (a number from 1-15)
These two values will combine to give you a color. The color represented by (y) is dependent on the Screen type and is not fixed. To make it easier to understand, let's use an analogy. One day Professor Clumsus tries to do an experiment. Lets say that he takes a gigantus hunk of potassium and drops it into some oil. Nothing happens, right? Now he takes the same hunk of potassium and drops it into the swimming pool. BOOM. So think of the water and the oil as different screen modes, and the potassium as the (y) variable. The point that we make here is that you get different results in different screen modes! So the only advice we can give you is: experiment! Its both fun and you learn at the same time. For example, Screen 12: Color 4: Print "Hi" will give you:
Hi

(P.S: The red reminds me of the day when my combine-harvester squashed 10 million tomatoes flat in Nebraska and my neighbours thought it was a volcano eruption. . .)

Back to Top


Basic Programming Tips

In this section of the lesson, we are going to give you some tips. No, please put your wallet back into your pocket. Not that sort of tips. Anyway, the first tip we are going to introduce is a very basic and simple form of line labeling. All you have to do is to put a number in front (e.g. 1) and then that line will be Line 1. Thus you can use the Goto command to jump to that line. We will introduce another type of line labeling further on, but we'll like you to learn this first. Look at the code below and study it carefully.

Raw Code

SCREEN 12: COLOR 15: CLS
1 PRINT "Hello World"
PRINT "Hello Worldly"
GOTO 1
END

Program Output

Hello World
Hello Worldly
Hello World
Hello Worldly
(An endless repeating sequence)

Dissection

What exactly happened in there? Why did the program repeat itself? Is it paranoid? Is it freaked out? Does it have to go to the mental hospital? Well, the culprit is this command called Goto. Right now hes in the city jail and unavailable for comments. But never mind, I'll comment for him. Notice that the 2nd line has a 1 before the Print command. This labels the line as Line 1. This is how QBasic interprets the program:

1 Print "Hello World" - Hmm, I've got to show a Hello World on the screen. Okay.
Print "Hello Worldly" - Now they tell me to display a Hello Worldly. Sure thing.
Goto 1 - Now where do I go? To Line 1. Where is Line 1?
(Search, Search - ahh, I see it: 1 Print "Hello World")
(QBasic goes to that line)
1 Print "Hello World" - . . .and so on. . .

In short, Goto x (where x is a line label) makes the program jump to that line.

Back to Top


Rem is very useful!

Now we go on to our next tip. This is a very cute and funny little command called Rem. Why is it so cute? Well, just type it in front of a line and you get nothing! I call it the cloak of invisibility. Here's a demostration of the magical abilities of the cloak, by the magician Microsoftus Qbasicus! First we take a Print command from the audience. . .

(Audience claps on cue. Print command goes up on stage.)

Print "Hello World!"
Now we add the Rem!
Rem Print "Hello World!"
Now we press F5 to run the program, and we get. . .

Nothing! Actually there should have been a "Hello World" there, but where did it go? Well, the Rem in front of the Print command cancelled the command out! So Rem cancels any code after it!

(The Print command's wife in the audience faints on cue. Medics carry her out.)

But why, you may ask, why do we need such a command? Well, for the simple reason that this feature is often used to include comments for other programmers who may study your code, or to include copyright notices. Thus it is very useful!

Back to Top


Basic Misc. Commands

We don't really know what to classify these commands as. We really don't. No wonder we failed our Science tests on grouping and classification. So now we've lumped these last handy commands from Chapter 1 together in a code sample, which we will endavour to implore you to learn.

Raw Code

REM (The first Program)
PRINT "Lala"
SLEEP 2
PRINT "Lala2"
SHELL "dir c:\"
END

REM (The second Program)
PRINT "Lala3"
SLEEP
SYSTEM

Program Output

(The first Program)

Lala
(Program waits for 2 seconds)
Lala2
(Program displays contents of drive C: like MS-DOS command dir.)
(QBasic displays automatic message "Press any key to Continue)

(The second Program)
Lala3
(Program freezes until any key is pressed)
(QBasic terminates to MS-DOS prompt)

Dissection

Here is a summary of the commands and what they do. Then study the code again and try to figure out what exactly happens when the program is run. After all, we don't want to spoon-feed you, do we? As your Science teacher said. . .

Sleep (y) - Sleep causes the program to halt temporarily. y is the number of seconds to pause the program. If y is omitted, the program pauses until a key is pressed.
System - System causes the program to halt and shuts down QBasic to return to MS-DOS Prompt.
End - End causes the program to halt and return to the QBasic Editor.
Shell "y" - Shell launches a secondary copy of MS-DOS Prompt from QBasic.

I'm sure you managed to figure it out! I really hope you did! If you didn't just try again, it's pretty easy to understand. Now learn the commands well, as they are among the most-used commands in QBasic!

NotePlease Note: Be very careful when using Shell! You may not launch a second copy of QBasic from Shell, and much memory will be hogged up by QBasic, resulting in possible Out of Memory Errors. We strongly advise you not to run any program from Shell. In the syntax, "y" is the command you want to execute. If "y" is omitted, the user recieves a C:\> Prompt.

Tip Sleep can be used as a "Press any Key to Continue" phrase in your programs. Just type in Print "Press Any Key to Continue" (Enter) Sleep.

Back to Top


QB Assignment

Create a QB Program in Screen Mode 12 that prints a never-ending message with a gap of 2 seconds between every message.
Click here to check your answer!

Well, that's it for Lesson 1! Chunk your empty Coke can into the dustbin and grab another from the fridge, we're going on to Lesson 2 tomorrow. . .

Back to Module 1

This is a line. -------- If you don't know what a line is, you're brain dead.

This is an imagemap menubar. Hey I think you know what an imagemap is.

Home Intro Lessons Library Reference About
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]

This is a line. -------- If you don't know what a line is, you're brain dead.

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.