What you will learn in this lesson
- Timer Commands
- Program Chaining
- Line Labeling
- FAT Commands
This is your last lesson for Module 1! After this its' holidays! In short, today is your last day of school before summer. The topics are basically Timer Commands using Timer, Program Chaining, which allows QBasic to switch programs, and Line Labeling using a semicolon, and FAT Commands. And no, they are not overweight because they had too many Mars Bars for lunch.
Timer Commands
Timer Commands. This isn't an instant automatic egg-timer--alarm-clock, it's the Timer Command! However, it can function as an egg-timer-cum-alarm-clock. Surprise, surprise! Basically, the command is used to time periods of time (pun not intended). After a certain period of time it will branch to a subroutine, something like the Error command, just that you don't have to wait for an error to go there! That's basically an explaination of what it does. And as for how it does it, have a look first at the code, and then we'll explain.

ON TIMER(1) GOSUB TimeUpdate
TIMER ON
CLS
PRINT "Time: "; TIME$
StartTime = TIMER
WHILE TimePast < 10
TimePast = TIMER - StartTime
WEND
END
TimeUpdate:
LOCATE 1, 7: PRINT TIME$
RETURN

(The program displays the present changing time here
on the screen for 10 seconds)
So, how did you feel after a look into the internals of time? If you're still a bit befuddled, let us explain the command to you. Well, the main command here is Timer. You already know that. But look at the first line carefully. It says:
ON TIMER(1) GOSUB TimeUpdate
Here, Gosub is just a command to branch to a line label. It will be covered in Module 2. But understand here that it directs the program flow to the line label TimeUpdate. The number in brackets (1) here refers to how long are the intervals between this branching to the Subroutine. Thus (1) will result in the program branching every second.
Now the next line. Timer On is the command to start the Timer working. That means, the program will start branching and timing the intervals only when it acknowledges this command. Similarly, Timer Off (if you put it there) will cause the timer to turn off and stop branching! It's pretty simple! I sure you know On and Off, don't you?
In case you're wondering about the Cls, it just performs its normal function here. So does the Print statement here. But the interesting part here is: Starttime = TIMER. Now why do we need this? Basically, for you to understand this, you need to know that the Timer mentioned here is an everchanging variable, for the simple reason that the present time changes all the time, right? (pun not intended here too!). So what this statement does is that it takes the present Timer value at that moment in time, and stores it in another variable, conviniently called StartTime. You'll see why we need this if we go on to the next few lines.
Now, here's the tricky part. In order to make the program stop after 10 seconds, the programmer has put in a While loop here. If you don't know what that is, go back to the earlier chapter on Advanced Control Flow! (and shame on you too for forgetting the lesson). Now, this While loop takes into account the variable TimePast, and if we look at the next line. . .
. . . we'll get to see why we need StartTime! TimePast = TIMER - StartTime! You see, TIMER was 1 when the program started. Therefore StartTime is 1. And thus TimePast at that point in time was 0 (1-1, in other words). But as the program runs, TIMER changes, right? For every second passed in real time, TIMER increases by 1, right? And the program, thanks to the While loop, will end when TimePast is 10 or more right? So TIMER will have to be 11 to end the program: TIMER - StartTime (11-1) = 10. You follow me? Thus. . . this program will effectively run for 10 seconds before it terminates! And how many lines of code did we take? Just 12!
Now for the subroutine. Actually this doesn't need much explaination. It just writes out the present time at the top of the screen, updating itself every second due to the Timer.
So that's it for Timer.
Program Chaining
Today we are going to introduce you to an entirely different concept - Program Chaining. Since it is new to you, naturally we will take some time to explain it to you first.
Now what is Program Chaining? Let's think on it by using an anology. Now let's say that you're an executioner at the infamous "Han-, no!-I. . ." Hilton, and you're using this new electric chair to execute people. Now one day you find that the mains electrical wire has somehow become broken and detached. So what does the enteprising executioner do? Ignoring the victim's pleas for mercy, you take a steel chain and connect the wires together. Thus the current can flow through now. And so you kill the prisoner. Unfortunately, you also kill yourself.
This hilarious example is set to illustrate the Chaining concept. Think of the mains wire as the Program, and the current as the Program Flow. Usually the Program Flow or current travels along the mains wire, right? But this Chaining concept, represented by the steel chain in the example, serves as a siphon of sorts. It siphons the current away into another mains wire. Thus the chaining concept is as follows: It diverts program Flow from one program to another. Thus you sorta jump from a program to a point in another program (which is not necessarily the start!). So the programs are "linked" together in this way, which gives rise to the name "Program Chaining".
Now that you know what Program Chaining is, we can start to learn the actual commands which use Program Chaining. Basically we are going to teach you 3 new commands here. They are the Run, Chain, and Common commands.
Run and Chain are the main commands that do the actually jumping around. Thus they are very strong and fit; you have to be strong and fit to jump around all the time. Here's the syntax for our strong and fit jumpers:
Chain file$
Run linenumber, file$
Now what does this mean? For Chain, you type in Chain and the variable file$. This variable is the path of the QBasic file which you want to run. And as for Run, you have two variables: linenumber and file$. File$ means the same thing as in Chain, and linenumber indicates the line which you wish the new program to start at. Now, you may be wondering, why have 2 commands for essentially the same function - Program Chaining? Won't just the Chain or Run command alone do just fine? Well, there are a few important differences between the two, and we are going to highlight them to you in a table form.
| Chain |
| Basically Chain just starts the program from there onwards, without closing any open files or erasing variables in memory. |
| Run |
| Run closes all open files and wipes memory space before executing the program. |
| Chain |
| You can thus transfer variables when you use Chain to the other program using Common............. |
| Run |
| As memory space is cleared, you can't do that with Run. |
| Chain |
| Chain will start the new program right from the beginning............................................................. |
| Run |
| Run can start the new program from a specified line number. |
And so, I hope you get your running and chaining up clear! Don't chain yourself up. . .
Finally we come to the end of the section. The last command related with chaining is the Common command. This new command is very closely related to the Chain command. It doesn't jump about, but instead defines the variables in the present program that should be passed over to the jumped-to program when it is chained. The syntax is plainly:
Common variables$
Variables$ is a list of common variables to be passed on, each seperated by commas. So you have: variable1, variable2, and so on. There! Finally! Joy to the world! Let us all dance in ecstasy (and no, we do not promote drugs, we are being literal here), for the end of the section is here!
FAT Commands

Fat should be avoided. That much is true. But FAT Commands in QBasic are definitely not avoidable. FAT Commands are basically commands that work directly with the disk FAT (File Allocation System), the backbone and structure of the disk, instead of working with the data from files loaded into memory like OpenThe commands to be reviewed here are Chdir, Mkdir, Rmdir, Kill, Name. . .As, and Files. These are the FAT commands that are accessible directly from QBasic. But what if you want to copy files? Well, other FAT commands not directly available here can be simulated by using the Shell command. Below we have an example of how to copy a file using Shell:
start:
REM This program is designed to show how Shell works simulating FAT
SCREEN 12: COLOR 15: CLS
ON ERROR GOTO errorsign
PRINT "No Spaces please!"
PRINT "Type in the syntax for the copy command."
PRINT "i.e `copy file.fle c:', where file.fle is your file
PRINT "and c:\ is your destination."
PRINT ""
INPUT "Input: "; a$
SHELL a$
PRINT ""
BEEP
PRINT "File Transfer successful!"
SLEEP
SYSTEM
errorsign:
CLS
PRINT "Either:"
PRINT "File not found, error 550"
PRINT "Or Input error, spaces found, error 102"
PRINT ""
PRINT "Press Any Key to Restart"
SLEEP
GOTO start
In the case of the variable a$, the user inputs the entire command just like in Dos, just that we are running his command from QBasic. So that's how you incorporate copying and moving functions in your programs. But enough of that. Back to the commands.
FAT Commands (Continued)

The first commands we are going to go through are the directory commands. These commands mainly deal with the creation, death and other functions of directories. Directories make up the backbone of your hard drive, and it is critical that you should have a deep understanding of their workings. The three commands are Mkdir, Chdir, and Rmdir. Mkdir makes a directory in the current directory, or in a specified directory. The syntax is so easy, that it doesn't need any explaination.
Syntax:
Mkdir directory$
Chdir changes the QBasic Working Directory. It works like the cd/ command in Dos. By default, the QBasic Working Directory is the root directory. However this can result in some problems if a programmer does not know where his users install the program to. This problem can be overcome by using a setup program that will save the path to a file, but many programmers prefer to use the Chdir command like this:
INPUT "Enter directory in which program installed: "; dir$
CHDIR dir$
OPEN "lala.dat"
. . .and so on.
The good thing about this is that you don't have to type in the full path of the program all the time while coding, and you don't know it anyway since it's not your computer and you didn't install it there. So all you have to do is to change the working directory from the default to the one the user specified! A great deal of energy has been saved!
We move on to the Rmdir command now. The Rmdir command is used to remove directories. However, there is one major flaw here! The program will crash if the directory specified has any subdirectories! To get around this problem, we use the Shell command again, coupled with the deltree command from Dos. We copy the deltree.exe file from the Dos directory, and in the same way as simulating the copy command, we simulate the quick and efficient removal of directories. So basically we don't like this command, but the syntax is:
Rmdir directory$
So you have learnt three commands for FAT, along with a few other tricks that we have discovered through experimentation ourselves. So what will we teach you now? Well, we've gone through Directory commands, right? Take it one level down. . .and we get File Commands! The three commands here are Kill, Name. . .As and Files.
You have met Kill before, when you were exterminating variables. Now we exterminate files. The syntax is the same as for variables:
Kill file$
Not much explanation needed. Now for Name. This new command to QBasic simply renames a file.
Name file$ AS newfilename$
I'm rushing, I know, but the end is in sight. Finally, the Files command. It simply displays the files in a directory. However, I prefer to use Shell with the Dos dir/p command. Figure it out yourselves. We do not spoonfeed people who are about to complete Module 1.
Files
This will display the contents of the present working directory. It works somewhat like dir/p. In fact, almost all of these QBasic FAT commands have Dos equivalents!
Finally, our assignment. The end has come.
QB Assignment
Finally, your large test for Module 1. Using all that you have learnt in Module 1, create a text-based RPG with a few levels. Use chaining and drawing commands. Let's say you have to find your way out of a maze in 10 minutes, amd you press buttons to direct the guy out. So, show the remaining time at the top of the screen. Include text descriptions, and a menu system and movement counter that shows the number of screens transversed. Finally, incorporate a "Registered Program Nag Screen"; read a value from a system file of any name you created in the same directory; if this value is not
that which is expected (the program is not registered), show a counter that makes them wait from 5000 to 0 before the program starts. Include appropriate text, error Subs and other related commands. This should keep you busy. . .have fun figuring this out! Last of all, for every 4 screens moved, make the guy have a 50% chance of meeting a random monster with Skill and Stamina, (give the guy a Skill and Stamina too) Make 2 random numbers from 1-20, add 1 to each of the guy's skill; whoever has the higher total deals 2 dmg to the opponent's Stamina. If the monster dies the guy continues, if not he dies and the game is over; show a you lose screen. Phew! Hard typing. . .
Guess what. . .do it yourself! It's your first independent program!
The End of Module 1
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]
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.