Microsoft QBasic
Click here for site map

Home
Intro
Lessons
Library
Reference
About
www.microsoft.com

This is a line. . .a short one

Lesson 4

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

What you will learn in this lesson
  • Error Syntax
  • Specific Variables and String Commands
  • Basic File Commands

Lesson 4

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

Refreshed after those graphic lessons? Today it's Lesson 4! We're approaching the end of the module now, but there's still some more to go. Today, as you can see above, we are going to focus on three important aspects of QBasic. These are namely Error Syntax, Specific Variables and Basic File Commands. It's a pretty short lesson, and you shouldn't be too tired after it. Well, here we go. (Did you remember to buckle that seat belt? If you didn't, you are now dead.)


Error Syntax

Error Syntax Explainations

Error Syntax is a very interesting subject. Basically it is a form of Advanced Control Flow. But unlike other Control Flow situations, it only applies to a specific situation, rather than applying itself actively in the program like If, Loop or For. Error instead branches Program Flow to a subroutine when it encounters an error. Also, Error Syntax is controlled by the use of this very simple command: Error. That's all there is to it. Just one word. Error. But what exactly is it? That's what we are going to find out.

So what is Error used for? Well, Error controls what QBasic does when it encounters an error in program execution. Let's say that you input in some code. But some of this code is carelessly wrong. So if that happens, you may just see an error box like this:

However, you will notice one thing: the program halts, and cannot continue. That's not very professional, yes? So what Error does is that it tells QBasic, if it encounters an error, it is to go to an Error Subroutine created by the programmer, which will handle the error in whatever way desired. The programmer may then decide to make the program terminate or just continue. So Error provides for the possibility of Out of Memory Errors, Wrong Input errors and other sloppy faults. Now that you know what it is and what it does, let's have a look at the syntax.

ON ERROR GOTO (x$)
(At the end of Error Routine)
RESUME NEXT (optional)

After the Error Sub

Now, the important part here is On Error. This tells QBasic to, when there is an error, to branch to the error subroutine. Here are explanations for the rest:

Goto (x$) - In this case, x$ is a line number or label. Remember Line Labeling in the earlier lessons? Well, x$ can be a number or a label here defining the line where the error subroutine starts. If it is 0, Error Syntax and Handling is turned off (i.e. QBasic will halt when it encouters an error). By default this is such.
Resume Next - This optional phrase tells QBasic to, after the end of the Error Subroutine, branch back to the line after that which caused the error.

And that's basically it for Error Syntax. Use it to display Error messages and such!

Back to Top


Specific Variables and String Commands

The concept of variables has been explained to you already in Lesson 2. Specific Variables are simply system variables which already contain a pre-set defined value that you cannot change.

Specific Variables: An Explaination

Think of it as something like the speed of your computer or a DOS system file; or even Io.sys or Msdos.sys or Win.com! Something that is important in QBasic - but you can't change it, right? (Unless you heartlessly dump your old one into the trash and buy a new one). So it's something like that. Anyway, all you have to do for this section is to learn them by heart! It's that easy. Have a look.

Actually, about the only two specific variables we are going to teach you are Date$ and Time$. These two variables are self-explainatory - they contain the present date and time respectively!. However, they can also be used to set the current system time by using the Input command:

Input DATE$
Input TIME$

So now the variables and the corresponding system time will be changed by the user. It can also be changed by the program itself in this way:

DATE$ = "blahblahblah"
TIME$ = "blahblahblah"

In this case, blahblahblah would be some numbers! You don't type blahblahblah there!!!

Back to Top


But, you may ask, if that's all we are going to teach you here, aren't you being ripped off? Well, no! We are going to teach you some interesting commands and statements here that deal with strings. Read on. . .

The commands we are going to teach you here are the Mid$, Left$, Right$, and Len. Basically, Mid$, Left$ and Right$ are related: they all get parts of a string and output it. Have a look:

Raw Code

A$ = "Where is Blahblahland?"
Print MID$(A$, 10, 4)
B$ = "Haha BooBoo"
Print LEFT$(B$, 4)
Print RIGHT$(B$, 6)
C$ = "YayYay"
Print LEN(C$)

And here is the output:

Program Output

Blah
Haha
BooBoo
6

Dissection

Basically let's give you an outline of the commands first. Mid$ is used to get a portion out of a string. For example, look at the code. Mid$ is of course the command. Then come brackets and the name of the variable to get the portion from, the character number in the string variable to start cutting the portion from, then the length of the portion. Thus from "Where is BlahBlahland?" with a start of 10 and a portion of 4, we get: "Blah". The "B" in "Blah" is the 10th character in the string, including spaces. The next three (4-1) characters after that are "lah", so QBasic adds them together to get "Blah". Get it?

The syntax is: Mid$(String$, number-to-start, length-of-portion)

Now for Left$ and Right$. These are used to grab portions from the left or the right of a string respectively. The syntax is Left$(String$, size-of-portion). It is the same for Right$. So this:

A$ = "HahaLulu"
LEFT$(A$, 4) 

This will give you "Haha". Finally, the Len command will return the size of the variable, or how long it is in numbers. For example, a 7-letter long variable will return a value of 7. The syntax is:

Len(String$)

I hope you understood that. Did you? (Yes!) Good. Now we move on to File Commands.

Back to Top


Basic File Commands

File Commands: A Diagram

The file commands we are explaining in this lesson are basically used for two things: reading from text files, and editing text files. We have decided to start off slowly with these basic concepts so that you won't get muddled up. Anyway, the command we are going to focus on here is Open. What does it do? Open a Time Travel Gate? No, Open simply opens (or loads) a file into memory so that you can play around with it. Here's what I'm talking about:

a$ = "This is Text! Yay! We love ASCII!"
OPEN "text1.txt" FOR OUTPUT AS #1
INPUT a$, #1
CLOSE #1

Let's go through the code so you have a better understanding of Open.

Open - Basically this is the main command to activate the process.
"text1.txt" - This is the filename of the file that QBasic should load.
For Output - For is a keyword that specifies the usage type. This is basically how the file is going to be used by QBasic. In place of Output (which is a usage type), you may enter Input, Binary or Append. Output means that you are going to write to the file. Input means that you are going to read from the file. And Append means that you are going to write to the file, but you won't erase the file contents by doing so. Binary will be covered in Module 2.
As #1 - This is a number that represents the file. No two files may have the same number, unless one already has been closed. #1 can be any other number, e.g. #2, #100. It just represents the file.
Input a$, #1 - In the case of Output and Append, the Input command used here writes the variable a$ to the file represented by the file number #1. In the case of Input, the command Input here will take the contents of the file #1 and store them in the variable a$.
Close #1 - This closes the file in QBasic.

So now that you've had some practice in this, think of ways in which this can be useful. Maybe a high score list? A Database? A Logbook? Think on! Maybe you can even combine this with the Input command!

NoteNote - You can use Open to check whether certain files are present in a directory. This can be useful if you want to ensure that your pgoram has been installed properly. Just use Open with the filename and an Error Syntax. If the file is not found then the Error Syntax will start!

Back to Top


QB Assignment

Create a Database that asks users for their Name, Age, Address and Occupation. Allow users to see other people's entries as well as make their own. If you're really ambitious, give them a password protect option! (Try to figure this out yourself!)
Click here to check your answer!

QB Assignment

Create a High Score List for a game that allows users to enter and read others' entries. Include an automatic annotation to every score; for example, if Barney gets only 1000 points, make the program add to the name "The Great Goon Barney got only 1000 points". On the contrary, if Pooh Bear gets 1000 000 points, make it say, "The wonderful, fantastic Pooh Bear got 1000 000 points. . ." I think you get the idea.
Click here to check your answer!

QB Assignment

Create a System Check Program that checks for specific files in a directory, e.g barney1.txt, barney2.txt. Display a Error message if the file is not found. Better still, improve on this and ask the user what files he wants to find. Turn it into a File Search program that searches for different files!
Click here to check your answer!

Have Fun!

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.