The magazine of the Melbourne PC User Group

Free programming!
Stephen Davey

If you have often wanted to get involved in programming, but thought that it might be too complicated or expensive, then perhaps you are overlooking the powerful programming tool you more than likely have already - BASIC. BASIC, in various forms, is the simple but very powerful programming language that comes free with Dos. Dos versions earlier than 5 came with BASIC, BASICA, Or GWBASIC. Dos 5 comes with QBASIC included. [Three other forms, QuickBasic, VisualBasic(Windows) and VisualBasic(Dos) are sold separate to Dos but are still quite inexpensive.

While there are some differences between each version, the basic concepts and language elements are generally the same. BASIC, BASICA and GWBASIC are very similar, QuickBasic and QBASIC likewise; and the two VisualBasic products were designed to cope with the "new" Guns (Graphics User Interfaces). For this series of articles I'll concentrate on the original BASICs, because if you have Dos then you have one of these. However, the code examples will also work with QBASIC and QuickBasic. I have used the word BASIC to refer to the earlier versions of BASIC only, and where applicable, specific reference is made to QBASIC, QuickBasic and VisualBasic.

Why Bother?

Perhaps you are thinking "Why should I bother to learn BASIC?". Well, there are many simple little applications or tasks that your computer night be able to do for you very easily if you could write the instructing program. Dos batch files are a starting point (and very simple to construct) but they are not as powerful or secure as a "proper program".

For instance, the simple sample program that I will use to later illustrate some of the BASIC language features, resulted from my own needs many years ago to print sticky labels. I needed a method to print out 5 or 6 copies of various simple address/disc labels. A word processor could have done the job, but I found by the time I altered the page setup, etc, I wanted something simple; i.e. type in the quantity required; the size of the label and then the required few lines of text.

I was able to write the BASIC program to do the job in five or ten minutes.

What Is a Program?

Before we examine the sample program, we need to discuss briefly just what is a program. In simplistic terms, Dos is capable of running (or taking instructions from) three types of "program" files.

Batch files (*.BAT) are simple ASCII files (generally created in a word processor) that contain various lines of DOS commands. DOS reads and carries out the commands one by one until it reaches the end of the file.

Executable (*.EXE) and Command (*.Com) files are files that have been "Complied" to create special series of instructions readable by the computer (machine readable - if we use a text editor to look at these files they appear as gibberish!)

BASIC is a program that lets us write, run and test program code (that we can read and understand). We can use (or run) this code contained in a *.BAS file within the BASIC environment, or we can "compile" it to produce a normal *.EXE file that can be used on any DOS machine without first having to run the BASIC program.

If you are using the older versions of BASIC then you will have to purchase a separate "Basic Compiler" program to turn your .BAS code files into .EXE files. While you can easily run your programs from BASIC without compiling them, the complied versions run faster and are easier to distribute. QuickBasic, QBAsic and VisualBasic users have the compiler already built in to the program.

Down to BASICS

First of all, start your particular version of the BASIC program (i.e. type BASIC, BASICA. QBASIC etc)

You can now type the lines of code required for your own unique program or if you wish type in the example code file exactly as it is, line for line.

Save your file by typing (for BASIC versions) SAVE "FileName.BAS". Then run the program by typing RUN .

The BASIC program works in a similar fashion to batch files in that the machine reads each line of the program one at a time carrying out the simple instruction then proceeding to the next line. It is quite easy to jump ahead or back to other lines if required.

In the BASIC versions each line of the program is numbered so the machine can keep track of where it is up to etc. QBAsic and QuickBasic versions do not require (but can use) line numbers and only need special "labels" if the program is required to jump over certain lines. (To cover as many BASIC versions as possible I have used line numbers in the examples. Users of QBASIC and QuickBasic can leave the numbers out.)

There are many different BASIC commands that carry out specific functions, but we will deal only with those that are required for this simple example. In following articles as we expand the scope and complexity of the program more commands will be required and explained then.

Let's Begin!

Here is a detailed explanation of what each line of code in the example does.
[ To make life easier, the source listing may be downloaded from this link. ]

Line 100, 110  
Both these lines start with the ' character which means that the line will be ignored by the program. This is a useful method for including comments or remarks within your code to help you remember what you did at a later date. The REM command can be used in place of the ' character.

You might wonder why I called the first line of the program line 100 and the second line 110. This method of line numbering (for the Basic versions) simply allows you to insert new lines at a later date without having to renumber all the lines. The program reads the first line of a program and then (unless instructed otherwise) proceeds to the next highest line number, so the lines do not have to be numbered 1, 2, 3, 4, 5 etc but can be numbered 100, 200, 203, 204, 300 etc.

Line 120
The CLS command simply dears the screen of anything that might be on it, and positions the cursor in the top left hand corner.

Line 130, 140
The PRINT command prints what follows the command (in this case "Label Printing Program) on the screen at the cursor position then moves the cursor down to the next line.

The PRINT command by itself prints a blank line on the screen and moves the cursor down. Lines 140, 160, 190, 220 and 290 are used to put a blank line between other items on the screen.

Line 150
The INPUT command first prints the comment following the command ("How many labels are required") on the screen then positions the cursor at the end of the comment and waits for the user to type something on the keyboard. Anything typed is displayed on the screen and when the ENTER key is pressed it is "stored" in the "variable" listed at the end of the INPUT command.

In this case I have called the variable Quantity and if for example the user (of the program) typed "5" then the value of Quantity becomes "5". Within certain limits, the name of the variable could have been anything such as QTY, NUMBER or even Q etc. Basic has several different types of variables, but in this program we will only be concerned with simple numeric variables and text variables.

To enable a variable to store text, its name must finish with the $ character (e.g. SurName$). Without the $ character the variable can only store numbers.

The variable Quantity will now store the value (typed at the keyboard) until the program is restarted or some specific program line alters its value.

Line 170
The IF command checks to see if something is true, and if so then caries out a specific function. Here, I have told the program that IF the value of the variable Quantity is equal to 0 then END. This means that if the program user responds to the INPUT question "How many labels are required" (Line 150) by just pressing ENTER or "0" then ENTER, then the program will end rather than trying to print "0" labels!

Lines 180-200
Here the question "Maximum text lines on label" is asked and the answer is stored in the variable MaxLines. Then a check (using the IF command) is made to end the program if the answer is zero.

Lines 210-230
The same procedure is used to find out how many characters are on the line. This is stored in the MaxCharacters variable.

Line 240
Stores the value for the number of blank lines between each label in the variable called LinesBetween.

So far, the INPUT command has been used to ask the program user four questions about number and size of the required labels and store the answers in four variables -

  • Quantity represents the number of labels required.
  • MaxLines is the maximum lines allowed for each label.
  • MaxCharacters is the maximum number of characters on each line.
  • LinesBetween is the gap between each label.
Next we need to find out from the user just what is to be printed on the label(s).

Lines 250-300
Because at the time the program is written, we do not know how many lines of text are required on the label, the FOR cornmand is used to repeat a series of steps or commands for a given number of times.

The FOR command consists of three parts -
  1. the FOR command line (Line 250)
  2. one or a group of several lines (Lines 260-290). (This group of lines will be repeated as many times as indicated in the FOR command line); and
  3. a NEXT command line (Line 300).
"FOR x = 1 to MaxLines" means that the block of lines (260-290) will be repeated until x equals the same value as MaxLines. x starts out with the value of 1 and is increased by 1 each time the block is completed (i.e. reaches the NEXT statement). If the user for example had answered "5" to the "Maximum text lines" question then the block would be repeated 5 times.

Line 260 prints Type in line plus the value of x on the screen. The first time the block is completed x equals 1 so Type in line 1 will appear on the screen. The next time the block is run Type in line 2 will appear etc.

Line 270
So the user has an indication of how many characters are allowed on each line, we want to print a row of dashes on the screen to represent the maximum number of characters allowed.

PRINT STRING$ command just prints a single character on the screen a certain number of times. In this case we want to print the "-" character as many times as the MaxCharacters value entered in line 210.

Line 280
The LINE INPUT command is similar to the INPUT command. The INPUT command is usually used to get the input of numbers or small amounts of text from the keyboard. The LINE INPUT command is used for larger amounts of text and in particular text strings that might contain commas etc which are not allowed with the INPUT command.

Here, rather that storing the result in a numeric variable such as Quantity we are going to use at text variable (note the $ character at the end of the name). Just to make things a bit more complex we are also going to use what is called an "array".

If we knew how many text lines were going to be used we could make up a name variable for each line, (e.g. TextLinel$, TextLine2 $, TextLine3 $ etc) but unfortunately we do not know how many lines the user might want to use. We can however create an *array* variable that is a group of variables all with the same basic name (in this case TextLine$(x)).

The first trine the block is run, and the value of x is 1, Line 280 effectively becomes LINE INPUT TextLine$ (1) . The next time it becomes LINE INPUT TextLine$(2),then LINE INPUT TextLine$ (3 ) and so on. The variables TextLine$(1),TextLine$(2) and TextLine$(3) are all separate variables with different values.

So, if the user entered "5" to the maximum line question (line 180) the FOR . . . NEXT block or loop (250-300) would be repeated creating the variables TextLine$(1) to TextLine$(5) to store the text for each line of the label

(Note -if the user wants more that 9 lines, the program will stop and display an error message. Future articles will deal with over coming this problem.)

Now we have the quantity and size of the labels, and the text to be printed on each line; lets now print the labels.

Lines 310-380
Here we have three separate FOR . . . NEXT commands or loops including two inside another one. So as to try and avoid confusion, the NEXT command (the finish of the block) has been identified with the same letter as used in the FOR command (this is not actually required by the program but helps make the program easier to read.)

The first FOR command (FOR x = 1 to Quantity) on line 310 includes all the lines up to the NEXT x command on line 380. This block of lines will be repeated the number of times that equals the value of Quantity, i.e. the number of labels the user wants to print,

The second, (FOR y = 1 to MaxLines) on line 320, is inside the first and includes all the lines up to the NEXT y command on line 340. This block of lines will be repeated the number of times that equals the value of MaxLines.

The LPRINT command on line 330 prints what comes after the command, not on the screen (as with the PRINT command) but to the Printer. So the first time the block is run line 330 will print the text string stored in the variable TextLine$(1) . TextLine(2) will be printed out the second time and TextLine$(3) the third etc.

The third FOR command, (FOR z = 1 to LinesBetween) on line 350, is also inside the first and includes the lines up to the NEXT z command on line 370. Line 360 simply prints a blank line (the LPRINT command without anything following) on the printer for the number of times in the LinesBetween" variable.

Line 400
The END. Having done its work, the program stops.

Please note that this example is very simple and crude and there is a lot that we can do (in a following article) to both make in work faster, be able to handle silly user errors, and look better on the screen.

So why not give it a go, you can t do much harm if you get something wrong!

Reprinted from the March 1993 issue of PC Update, the magazine of Melbourne PC User Group, Australia
 

[About Melbourne PC User Group]