Well Come to Professional Programmers

Learning Goal

Learning is a process to acquire, enhance, or make changes in one's knowledge. Learning Module presents essential and masks non-essential pieces of knowledge. It is self paced and presents required links to important examples and related terminologies essentially required for examination centric study. The module is focused on examination system in vogue in Pakistan and helps student acquire maximum marks in limited time of study.

GW-BASIC COUNTER

Sunday, April 25, 2010

Welcome to GW-BASIC



Chapter 1
Welcome to GW-BASIC




Notational Conventions


Microsoft® GW-BASIC® is a simple, easy-to-learn, easy-to-use computer programming language with English-like statements and mathematical notations. With GW-BASIC you will be able to write both simple and complex programs to run on your computer. You will also be able to modify existing software that is written in GW-BASIC.


This guide is designed to help you use the GW-BASIC Interpreter with the MS-DOS® operating system. Section 1.5 lists resources that will teach you how to program.



1.1 System Requirements


This version of GW-BASIC requires MS-DOS version 3.2 or later.



1.2 Preliminaries


Your GW-BASIC files will be on the MS-DOS diskette located at the back of the MS-DOS User's Reference. Be sure to make a working copy of the diskette before you proceed.





Note


This manual is written for the user familiar with the MS-DOS operating system. For more information on MS-DOS, refer to the Microsoft MS-DOS 3.2 User's Guide and User's Reference.





1.3 Notational Conventions


Throughout this manual, the following conventions are used to distinguish elements of text:


Used for commands, options, switches, and literal portions of syntax that must appear exactly as shown.


Used for filenames, variables, and placeholders that represent the type of text to be entered by the user.


Used for sample command lines, program code and examples, and sample sessions.


Used for keys, key sequences, and acronyms.


Brackets surround optional command-line elements.




Getting Started with GW-BASIC



Chapter 2
Getting Started with GW-BASIC



This chapter describes how to load GW-BASIC into your system. It also explains the two different types of operation modes, line formats, and the various elements of GW-BASIC.



2.1 Loading GW-BASIC


To use the GW-BASIC language, you must load it into the memory of your computer from your working copy of the MS-DOS diskette. Use the following procedure:



  1. Turn on your computer.

  2. Insert your working copy of the MS-DOS diskette into Drive A of your computer, and press RETURN.

  3. Type the following command after the A> prompt, and press RETURN:


    gwbasic




Once you enter GW-BASIC, the GW-BASIC prompt, Ok, will replace the MS-DOS prompt, A>.


On the screen, the line XXXXX Bytes Free indicates how many bytes are available for use in memory while using GW-BASIC.


The function key (F1–F10) assignments appear on the bottom line of the screen. These function keys can be used to eliminate key strokes and save you time. Chapter 4, "The GW-BASIC Screen Editor," contains detailed information on function keys.



2.2 Modes of Operation


Once GW-BASIC is initialized (loaded), it displays the Ok prompt. Ok means GW-BASIC is at command level; that is, it is ready to accept commands. At this point, GW-BASIC may be used in either of two modes: direct mode or indirect mode.



2.2.1 Direct Mode


In the direct mode, GW-BASIC statements and commands are executed as they are entered. Results of arithmetic and logical operations can be displayed immediately and/or stored for later use, but the instructions themselves are lost after execution. This mode is useful for debugging and for using GW-BASIC as a calculator for quick computations that do not require a complete program.



2.2.2 Indirect Mode


The indirect mode is used to enter programs. Program lines are always preceded by line numbers, and are stored in memory. The program stored in memory is executed by entering the RUN command.



2.3 The GW-BASIC Command Line Format


The GW-BASIC command line lets you change the environment or the conditions that apply while using GW-BASIC.



2.4 GW-BASIC Statements, Functions, Commands, and Variables


A GW-BASIC program is made up of several elements: keywords, commands, statements, functions, and variables.



2.4.1 Keywords


GW-BASIC keywords, such as PRINT, GOTO, and RETURN have special significance for the GW-BASIC Interpreter. GW-BASIC interprets keywords as part of statements or commands.


Keywords are also called reserved words. They cannot be used as variable names, or the system will interpret them as commands. However, keywords may be embedded within variable names.


Keywords are stored in the system as tokens (1- or 2-byte characters) for the most efficient use of memory space.



2.4.2 Commands


Commands and statements are both executable instructions. The difference between commands and statements is that commands are generally executed in the direct mode, or command level of the interpreter. They usually perform some type of program maintenance such as editing, loading, or saving programs. When GW-BASIC is invoked and the GW-BASIC prompt, Ok, appears, the system assumes command level.



2.4.3 Statements


A statement, such as ON ERROR...GOTO, is a group of GW-BASIC keywords generally used in GW-BASIC program lines as part of a program. When the program is run, statements are executed when, and as, they appear.



2.4.4 Functions


The GW-BASIC Interpreter performs both numeric and string functions.



2.4.4.1 Numeric Functions


The GW-BASIC Interpreter can perform certain mathematical (arithmetical or algebraic) calculations. For example, it calculates the sine (SIN), cosine (COS), or tangent (TAN) of angle x.


Unless otherwise indicated, only integer and single-precision results are returned by numeric functions.



2.4.4.2 String Functions


String functions operate on strings. For example, TIME$ and DATE$ return the time and date known by the system. If the current time and date are entered during system start up, the correct time and date are given (the internal clock in the computer keeps track).



2.4.4.3 User-Defined Functions


Functions can be user-defined by means of the DEF FN statement. These functions can be either string or numeric.



2.4.5 Variables


Certain groups of alphanumeric characters are assigned values and are called variables. When variables are built into the GW-BASIC program they provide information as they are executed.


For example, ERR defines the latest error which occurred in the program; ERL gives the location of that error. Variables can also be defined and/or redefined by the user or by program content.


All GW-BASIC commands, statements, functions, and variables are individually described in the GW-BASIC User's Reference.



2.5 Line Format


Each of the elements of GW-BASIC can make up sections of a program that are called statements. These statements are very similar to sentences in English. Statements are then put together in a logical manner to create programs. The GW-BASIC User's Reference describes all of the statements available for use in GW-BASIC.


In a GW-BASIC program, lines have the following format:


nnnnn statement[statements]

nnnnn is a line number


statement is a GW-BASIC statement.


A GW-BASIC program line always begins with a line number and must contain at least one character, but no more than 255 characters. Line numbers indicate the order in which the program lines are stored in memory, and are also used as references when branching and editing. The program line ends when you press the RETURN key.


Depending on the logic of your program, there may be more than one statement on a line. If so, each must be separated by a colon (:). Each of the lines in a program should be preceded by a line number. This number may be any whole integer from 0 to 65529. It is customary to use line numbers such as 10, 20, 30, and 40, in order to leave room for any additional lines that you may wish to include later. Since the computer will run the statements in numerical order, additional lines needn't appear in consecutive order on the screen: for example, if you entered line 35 after line 60, the computer would still run line 35 after line 30 and before line 40. This technique may save your reentering an entire program in order to include one line that you have forgotten.


The width of your screen is 80 characters. If your statement exceeds this width, the cursor will wrap to the next screen line automatically. Only when you press the RETURN key will the computer acknowledge the end of the line. Resist the temptation to press RETURN as you approach the edge of the screen (or beyond). The computer will automatically wrap the line for you. You can also press CTRL-RETURN, which causes the cursor to move to the beginning of the next screen line without actually entering the line. When you press RETURN, the entire logical line is passed to GW-BASIC for storage in the program.


In GW-BASIC, any line of text that begins with a numeric character is considered a program line and is processed in one of three ways after the RETURN key is pressed:



  • A new line is added to the program. This occurs if the line number is legal (within the range of 0 through 65529), and if at least one alpha or special character follows the line number in the line.

  • An existing line is modified. This occurs if the line number matches the line number of an existing line in the program. The existing line is replaced with the text of the newly-entered line. This process is called editing.



2.6 Returning to MS-DOS


Before you return to MS-DOS, you must save the work you have entered under GW-BASIC, or the work will be lost.


To return to MS-DOS, type the following after the Ok prompt, and press RETURN:


system

The system returns to MS-DOS, and the A> prompt appears on your screen.



Reviewing and Practicing GW-BASIC



Chapter 3
Reviewing and Practicing GW-BASIC




The practice sessions in this chapter will help you review what you have learned. If you have not done so, this is a good time to turn on your computer and load the GW-BASIC Interpreter.



3.1 Example for the Direct Mode


You can use your computer in the direct mode to perform fundamental arithmetic operations. GW-BASIC recognizes the following symbols as arithmetic operators:









Operation

GW-BASIC Operator

Addition

+

Subtraction

-

Multiplication

*

Division

/



To enter a problem, respond to the Ok prompt with a question mark (?), followed by the statement of the problem you want to solve, and press the RETURN key. In GW-BASIC, the question mark can be used interchangeably with the keyword PRINT. The answer is then displayed.


Type the following and press the RETURN key:



?2+2


GW-BASIC will display the answer on your screen:



?2+2
4
Ok


To practice other arithmetic operations, replace the + sign with the desired operator.


The GW-BASIC language is not restricted to arithmetic functions. You can also enter complex algebraic and trigonometric functions. The formats for these functions are provided in Chapter 6, "Constants, Variables, Expressions and Operators."



3.2 Examples for the Indirect Mode


The GW-BASIC language can be used for functions other than simple algebraic calculations. You can create a program that performs a series of operations and then displays the answer. To begin programming, you create lines of instructions called statements. Remember that there can be more than one statement on a line, and that each line is preceded by a number.


For example, to create the command PRINT 2+3 as a statement, type the following:



10 print 2+3


When you press the RETURN key, the cursor shifts to the next line, but nothing else happens. To make the computer perform the calculation, type the following and press the RETURN key:



run


Your screen should look like this:



Ok
10 print 2+3
run
5
Ok


You have just written a program in GW-BASIC.


The computer reserves its calculation until specifically commanded to continue (with the RUN command). This allows you to enter more lines of instruction. When you type the RUN command, the computer does the addition and displays the answer.


The following program has two lines of instructions. Type it in:



10 x=3
20 print 2+x


Now use the RUN command to have the computer calculate the answer.


Your screen should look like this:



Ok
10 x=3
20 print 2+x
run
5
Ok


The two features that distinguish a program from a calculation are



  1. The numbered lines

  2. The use of the run command


These features let the computer know that all the statements have been typed and the computation can be carried out from beginning to end. It is the numbering of the lines that first signals the computer that this is a program, not a calculation, and that it must not do the actual computation until the RUN command is entered.


In other words, calculations are done under the direct mode. Programs are written under the indirect mode.


To display the entire program again, type the LIST command and press the RETURN key:



list


Your screen should look like this:



Ok
10 x=3
20 print 2+x
run
Ok
5
Ok
list
10 X=3
20 PRINT 2+X
Ok


You'll notice a slight change in the program. The lowercase letters you entered have been converted into uppercase letters. The LIST command makes this change automatically.



3.3 Function Keys


Function keys are keys that have been assigned to frequently-used commands. The ten function keys are located on the left side of your keyboard. A guide to these keys and their assigned commands appears on the bottom of the GW-BASIC screen. To save time and keystrokes, you can press a function key instead of typing a command name.


For example, to list your program again, you needn't type the LIST command; you can use the function key assign to it, instead:



  • Press the F1 key.

  • Press RETURN.


Your program should appear on the screen.


To run the program, simply press the F2 key, which is assigned to the RUN command.


As you learn more commands, you'll learn how to use keys F3 through F10. Chapter 4, "The GW-BASIC Screen Editor," contains more information about keys used in GW-BASIC.



3.4 Editing Lines


There are two basic ways to change lines. You can



  • Delete and replace them

  • Alter them with the EDIT command


To delete a line, simply type the line number and press the RETURN key. For example, if you type 12 and press the RETURN key, line number 12 is deleted from your program.


To use the EDIT command, type the command EDIT, followed by the number of the line you want to change. For example, type the following, and press the RETURN key:



edit 10


You can then use the following keys to perform editing:












Key Function
CURSOR UP,

CURSOR DOWN,

CURSOR LEFT,

CURSOR RIGHT
Moves the cursor within the statement
BACKSPACE Deletes the character to the left of the cursor
DELETE (DEL) Deletes the current character
INSERT (INS) Lets you insert characters to the left of the cursor.


For example, to modify statement (line) 10 to read x=4, use the CURSOR-RIGHT control key to move the cursor under the 3, and then type a 4. The number 4 replaces the number 3 in the statement.


Now press the RETURN key, and then the F2 key.


Your screen displays the following:



Ok
10 X=4
RUN
6
Ok


3.5 Saving Your Program File


Creating a program is like creating a data file. The program is a file that contains specific instructions, or statements, for the computer. In order to use the program again, you must save it, just as you would a data file.


To save a file in GW-BASIC, use the following procedure:



  1. Press the F4 key. The command word SAVE" appears on your screen.

  2. Type a name for the program, and press the RETURN key. The file is saved under the name you specified.


To recall a saved file, use the following procedure:



  1. Press the F3 key. The command load LOAD" appears on your screen.

  2. Type the name of the file.

  3. Press RETURN.


The file is loaded into memory, and ready for you to list, edit, or run.


The GW-BASIC Screen Editor



Chapter 4
The GW-BASIC Screen Editor



You can edit GW-BASIC program lines as you enter them, or after they have been saved in a program file.



4.1 Editing Lines in New Files


If an incorrect character is entered as a line is being typed, it can be deleted with the BACKSPACE or DEL keys, or with CTRL-H. After the character is deleted, you can continue to type on the line.


The ESC key lets you delete a line from the screen that is in the process of being typed. In other words, if you have not pressed the RETURN key, and you wish to delete the current line of entry, press the ESC key.


To delete the entire program currently residing in memory, enter the NEW command. NEW is usually used to clear memory prior to entering a new program.



4.2 Editing Lines in Saved Files


After you have entered your GW-BASIC program and saved it, you may discover that you need to make some changes. To make these modifications, use the LIST statement to display the program lines that are affected:



  1. Reload the program.

  2. Type the LIST command, or press the F1 key.

  3. Type the line number, or range of numbers, to be edited.


The lines will appear on your screen.



4.2.1 Editing the Information in a Program Line


You can make changes to the information in a line by positioning the cursor where the change is to be made, and by doing one of the following:



  • Typing over the characters that are already there.

  • Deleting characters to the left of the cursor, using the BACKSPACE key.

  • Deleting characters at the cursor position using the DEL key on the number pad.

  • Inserting characters at the cursor position by pressing the INS key on the number pad. This moves the characters following the cursor to the right making room for the new information.

  • Adding to or truncating characters at the end of the program line.


If you have changed more than one line, be sure to press RETURN on each modified line. The modified lines will be stored in the proper numerical sequence, even if the lines are not updated in numerical order.





Note


A program line will not actually have changes recorded within the GW-BASIC program until the RETURN key is pressed with the cursor positioned somewhere on the edited line.





You do not have to move the cursor to the end of the line before pressing the RETURN key. The GW-BASIC Interpreter remembers where each line ends, and transfers the whole line, even if RETURN is pressed while the cursor is located in the middle or at the beginning of the line.


To truncate, or cut off, a line at the current cursor position, type CTRL-END or CTRL-E, followed by pressing the RETURN key.


If you have originally saved your program to a program file, make sure that you save the edited version of your program. If you do not do this, your modifications will not be recorded.



4.3 Special Keys


The GW-BASIC Interpreter recognizes nine of the numeric keys on the right side of your keyboard. It also recognizes the BACKSPACE key, ESC key, and the CTRL key. The following keys and key sequences have special functions in GW-BASIC:































































































Key Function
BACKSPACE or CTRL-H Deletes the last character typed, or deletes the character to the left of the cursor. All characters to the right of the cursor are moved left one position. Subsequent characters and lines within the current logical line are moved up as with the DEL key.
CTRL-BREAK or CTRL-C Returns to the direct mode, without saving changes made to the current line. It will also exit auto line-numbering mode.
CTRL-CURSOR LEFT or CTRL-B Moves the cursor to the beginning of the previous word. The previous word is defined as the next character to the left of the cursor in the set A to Z or in the set 0 to 9.
CTRL-CURSOR RIGHT or CTRL-F Moves the cursor to the beginning of the next word. The next word is defined as the next character to the right of the cursor in the set A to Z or in the set 0 to 9. In other words, the cursor moves to the next number or letter after a blank or other special character.
CURSOR DOWN or CTRL--   Moves the cursor down one line on the screen.
CURSOR LEFT or CTRL-]   Moves the cursor one position left. When the cursor is advanced beyond the left edge of the screen, it will wrap to the right side of the screen on the preceding line.
CURSOR RIGHT or CTRL-\ Moves the cursor one position right. When the cursor is advanced beyond the right edge of the screen, it will wrap to the left side of the screen on the following line.
CURSOR UP or CTRL-6 Moves the cursor up one line on the screen.
CTRL-BACKSPACE or DEL Deletes the character positioned over the cursor. All characters to the right of the one deleted are then moved one position left to fill in where the deletion was made.If a logical line extends beyond one physical line, characters on subsequent lines are moved left one position to fill in the previous space, and the character in the first column of each subsequent line is moved up to the end of the preceding line.DEL (delete) is the opposite of INS (insert). Deleting text reduces logical line length.
CTRL-END or CTRL-E Erases from the cursor position to the end of the logical line. All physical screen lines are erased until the terminating RETURN is found.
CTRL-N or END Moves the cursor to the end of the logical line. Characters typed from this position are added to the line.
CTRL-RETURN or CTRL-J Moves the cursor to the beginning of the next screen line. This lets you create logical program lines which are longer than the physical screen width. Logical lines may be up to 255 characters long. This function may also be used as a line feed.
CTRL-M or RETURN  Enters a line into the GW-BASIC program. It also moves the cursor to the next logical line.
CTRL-[ or ESC Erases the entire logical line on which the cursor is located.
CTRL-G Causes a beep to emit from your computer's speaker.
CTRL-K or HOME Moves the cursor to the upper left corner of the screen. The screen contents are unchanged.
CTRL-HOME or CTRL-L Clears the screen and positions the cursor in the upper left corner of the screen.
CTRL-R or INS

Turns the Insert Mode on and off. Insert Mode is indicated by the cursor blotting the lower half of the character position. In Graphics Mode, the normal cursor covers the whole character position. When Insert Mode is active, only the lower half of the character position is blanked by the cursor.


When Insert Mode is off, characters typed replace existing characters on the line. The SPACEBAR erases the character at the current cursor position and moves the cursor one character to the right. The CURSOR-RIGHT key moves the cursor one character to the right, but does not delete the character.


When Insert Mode is off, pressing the TAB key moves the cursor over characters until the next tab stop is reached. Tab stops occur every eight character positions.


When Insert Mode is on, characters following the cursor are moved to the right as typed characters are inserted before them at the current cursor position. After each keystroke, the cursor moves one position to the right. Line wrapping is observed. That is, as characters move off the right side of the screen, they are inserted from the left on subsequent lines. Insertions increase logical line length.


When Insert Mode is on, pressing the TAB key causes blanks to be inserted from current cursor position to the next tab stop. Line wrapping is observed as above.

CTRL-NUM LOCK or CTRL-S Places the computer in a pause state. To resume operation, press any other key.
CTRL-PRTSC Causes characters printed on the screen to echo to the line printer (lpt1:). In other words, you will be printing what you type on the screen. Pressing CTRL-PRTSC a second time turns off the echoing of characters to lpt1:.
SHIFT + PRTSC Sends the current screen contents to the printer, effectively creating a snapshot of the screen.
CTRL-I or TAB Moves the cursor to the next tab stop. Tab stops occur every eight columns.


4.4 Function Keys


Certain keys or combinations of keys let you perform frequently-used commands or functions with a minimum number of keystrokes. These keys are called function keys.


The special function keys that appear on the left side of your keyboard can be temporarily redefined to meet the programming requirements and specific functions that your program may require.


Function keys allow rapid entry of as many as 15 characters into a program with one keystroke. These keys are located on the left side of your keyboard and are labeled F1 through F10. GW-BASIC has already assigned special functions to each of these keys. You will notice that after you load GW-BASIC, these special key functions appear on the bottom line of your screen. These key assignments have been selected for you as some of the most frequently used commands.


Initially, the function keys are assigned the following special functions:



Table 4.1


GW-BASIC Function Key Assignments









KeyFunction KeyFunction
F1LIST  F6 ,"LPT1:"¿
F2RUN¿ F7TRON¿
F3LOAD" F8TROFF¿
F4SAVE" F9KEY
F5CONT¿ F10SCREEN 0,0,0¿






Note


The ¿ following a function indicates that you needn't press the RETURN key after the function key. The selected command will be immediately executed.





If you choose, you may change the assignments of these keys. Any one or all of the 10 function keys may be redefined. For more information, see the KEY and ON KEY statements in the GW-BASIC User's Reference.


Creating and Using Files



Chapter 5
Creating and Using Files



There are two types of files in MS-DOS systems:



  • Program files, which contain the program or instructions for the computer

  • Data files, which contain information used or created by program files



5.1 Program File Commands


The following are the commands and statements most frequently used with program files. The GW-BASIC User's Reference contains more information on each of them.


SAVE filename[,a][,p]

Writes to diskette the program currently residing in memory.


LOAD filename[,r]

Loads the program from a diskette into memory. LOAD deletes the current contents of memory and closes all files before loading the program.


RUN filename[,r]

Loads the program from a diskette into memory and runs it immediately. RUN deletes the current contents of memory and closes all files before loading the program.


MERGE filename

Loads the program from a diskette into memory, but does not delete the current program already in memory.


KILL filename

Deletes the file from a diskette. This command can also be used with data files.


NAME old filename AS new filename

Changes the name of a diskette file. Only the name of the file is changed. The file is not modified, and it remains in the same space and position on the disk. This command can also be used with data files.




5.2 Data Files


GW-BASIC programs can work with two types of data files:



  • Sequential files

  • Random access files


Sequential files are easier to create than random access files, but are limited in flexibility and speed when accessing data. Data written to a sequential file is a series of ASCII characters. Data is stored, one item after another (sequentially), in the order sent. Data is read back in the same way.


Creating and accessing random access files requires more program steps than sequential files, but random files require less room on the disk, because GW-BASIC stores them in a compressed format in the form of a string.


The following sections discuss how to create and use these two types of data files.



5.2.1 Creating a Sequential File


The following statements and functions are used with sequential files:









CLOSE LOF
EOF OPEN
INPUT# PRINT#
LINE INPUT#PRINT# USING
LOC UNLOCK
LOCK WRITE#

The following program steps are required to create a sequential file and access the data in the file:



  1. Open the file in output (O) mode. The current program will use this file first for output:


    OPEN "O",#1,"filename"


  2. Write data to the file using the PRINT# or WRITE# statement:


    PRINT#1,A$
    PRINT#1,B$
    PRINT#1,C$


  3. To access the data in the file, you must close the file and reopen it in input (I) mode:


    CLOSE #1
    OPEN "I",#1,"filename

  4. Use the INPUT# or LINE INPUT# statement to read data from the sequential file into the program:


    INPUT#1,X$,Y$,Z$



Example 1 is a short program that creates a sequential file, data, from information input at the terminal.



Example 1


10 OPEN "O",#1,"DATA"
20 INPUT "NAME";N$
30 IF N$="DONE" THEN END
40 INPUT "DEPARTMENT";D$
50 INPUT "DATE HIRED";H$
60 PRINT#1,N$;","D$",";H$
70 PRINT:GOTO 20
RUN
NAME? MICKEY MOUSE
DEPARTMENT? AUDIO/VISUAL AIDS
DATE HIRED? 01/12/72
NAME? SHERLOCK HOLMES
DEPARTMENT? RESEARCH
DATE HIRED? 12/03/65
NAME? EBENEEZER SCROOGE
DEPARTMENT? ACCOUNTING
DATE HIRED? 04/27/78
NAME? SUPER MANN
DEPARTMENT? MAINTENANCE
DATE HIRED? 08/16/78
NAME? DONE
OK


5.2.2 Accessing a Sequential File


The program in Example 2 accesses the file data, created in the program in Example 1, and displays the name of everyone hired in 1978.



Example 2


10 OPEN "I",#1,"DATA"
20 INPUT#1,N$,D$,H$
30 IF RIGHT$(H$,2)="78" THEN PRINT N$
40 GOTO 20
50 CLOSE #1
RUN
EBENEEZER SCROOGE
SUPER MANN
Input past end in 20
Ok

The program in Example 2 reads, sequentially, every item in the file. When all the data has been read, line 20 causes an "Input past end" error. To avoid this error, insert line 15, which uses the EOF function to test for end of file:


15 IF EOF(1) THEN END

and change line 40 to GOTO 15.


A program that creates a sequential file can also write formatted data to the diskette with the PRINT# USING statement. For example, the following statement could be used to write numeric data to diskette without explicit delimiters:


PRINT#1, USING"####.##,"; A, B, C, D

The comma at the end of the format string serves to separate the items in the disk file.


The LOC function, when used with a sequential file, returns the number of 128-byte records that have been written to or read from the file since it was opened.



5.2.3 Adding Data to a Sequential File


When a sequential file is opened in O mode, the current contents are destroyed. To add data to an existing file without destroying its contents, open the file in append (A) mode.


The program in Example 3 can be used to create, or to add onto a file called names. This program illustrates the use of LINE INPUT. LINE INPUT will read in characters until it sees a carriage return indicator, or until it has read 255 characters. It does not stop at quotation marks or commas.


Example 3


10 ON ERROR GOTO 2000
20 OPEN "A", #1, "NAMES"
110 REM ADD NEW ENTRIES TO FILE
120 INPUT "NAME"; N$
130 IF N$="" THEN 200 `CARRIAGE RETURN EXITS INPUT LOOP
140 LINE INPUT "ADDRESS? "; A$
150 LINE INPUT "BIRTHDAY? "; B$
160 PRINT#1, N$
170 PRINT#1, A$
180 PRINT#1, B$
190 PRINT: GOTO 120
200 CLOSE #1
2000 ON ERROR GOTO 0

In lines 10 and 2000 the ON ERROR GOTO statement is being used. This statement enables error trapping and specifies the first line (2000) of the error handling subroutine. Line 10 enables the error handling routine. Line 2000 disables the error handling routine and is the point where GW-BASIC branches to print the error messages.



5.3 Random Access Files


Information in random access files is stored and accessed in distinct, numbered units called records. Since the information is called by number, the data can be called from any disk location; the program needn't read the entire disk, as when seeking sequential files, to locate data. GW-BASIC supports large random files. The maximum logical record number is 232 -1.


The following statements and functions are used with random files:









CLOSEFIELDMKI$
CVDLOCMKS$
CVILOCKOPEN
CVSLOFPUT
EOFLSET/RSETUNLOCK
ETMKD$ 


5.3.1 Creating a Random Access File


The following program steps are required to create a random data file:



  1. Open the file for random access (R) mode. The following example specifies a record length of 32 bytes. If the record length is omitted, the default is 128 bytes.


    OPEN "R", #1, "filename", 32


  2. Use the FIELD statement to allocate space in the random buffer for the variables that will be written to the random file:


    FIELD#1, 20 AS N$, 4 AS A$, 8 AS P$

    In this example, the first 20 positions (bytes) in the random file buffer are allocated to the string variable N$. The next 4 positions are allocated to A$; the next 8 to P$.



  3. Use LSET or RSET to move the data into the random buffer fields in left- or right-justified format (L=left SET;R=right SET). Numeric values must be made into strings when placed in the buffer. MKI$ converts an integer value into a string; MKS$ converts a single-precision value, and MKD$ converts a double-precision value.


    LSET N$=X$
    LSET A$=MKS$(AMT)
    LSET P$=TEL$


  4. Write the data from the buffer to the diskette using the PUT statement:


    PUT #1, CODE%



The program in Example 4 takes information keyed as input at the terminal and writes it to a random access data file. Each time the PUT statement is executed, a record is written to the file. In the example, the 2-digit CODE% input in line 30 becomes the record number.





Note


Do not use a fielded string variable in an INPUT or LET statement. This causes the pointer for that variable to point into string space instead of the random file buffer.





Example 4



10 OPEN "R", #1, "INFOFILE", 32
20 FIELD#1, 20 AS N$, 4 AS A$, 8 AS P$
30 INPUT "2-DIGIT CODE"; CODE%
40 INPUT "NAME"; X$
50 INPUT "AMOUNT"; AMT
60 INPUT "PHONE"; TEL$: PRINT
70 LSET N$=X$
80 LSET A$=MKS$(AMT)
90 LSET P$=TEL$
100 PUT #1, CODE%
110 GOTO 30


5.3.2 Accessing a Random Access File


The following program steps are required to access a random file:



  1. Open the file in R mode:


    OPEN "R", #1, "filename", 32


  2. Use the FIELD statement to allocate space in the random buffer for the variables that will be read from the file:


    FIELD, #1, 20 AS N$, 4 AS A$, 8 AS P$

    In this example, the first 20 positions (bytes) in the random file buffer are allocated to the string variable N$. The next 4 positions are allocated to A$; the next 8 to P$.




    Note


    In a program that performs both INPUT and OUTPUT on the same random file, you can often use just one OPEN statement and one FIELD statement.





  3. Use the GET statement to move the desired record into the random buffer.


    GET #1, CODE%

    The data in the buffer can now be accessed by the program.



  4. Convert numeric values back to numbers using the convert functions: CVI for integers, CVS for single-precision values, and CVD for double-precision values.


    PRINT N$
    PRINT CVS(A$)
    .
    .
    .



The program in Example 5 accesses the random file, infofile, that was created in Example 4. By inputting the 3-digit code, the information associated with that code is read from the file and displayed.



Example 5


10 OPEN "R",#1,"INFOFILE",32
20 FIELD #1, 20 AS N$, 4 AS A$, 8 AS P$
30 INPUT "2-DIGIT CODE";CODE%
40 GET #1, CODE%
50 PRINT N$
60 PRINT USING "$$###.##";CVS(A$)
70 PRINT P$:PRINT
80 GOTO 30

With random files, the LOC function returns the current record number. The current record number is the last record number used in a GET or PUT statement. For example, the following line ends program execution if the current record number in file#1 is higher than 99:


IF LOC(1)>99 THEN END

Example 6 is an inventory program that illustrates random file access. In this program, the record number is used as the part number, and it is assumed that the inventory will contain no more than 100 different part numbers.


Lines 900-960 initialize the data file by writing CHR$(255) as the first character of each record. This is used later (line 270 and line 500) to determine whether an entry already exists for that part number.


Lines 130-220 display the different inventory functions that the program performs. When you type in the desired function number, line 230 branches to the appropriate subroutine.



Example 6


120 OPEN"R",#1,"INVEN.DAT",39
125 FIELD#1,1 AS F$,30 AS D$, 2 AS Q$,2 AS R$,4 AS P$
130 PRINT:PRINT "FUNCTIONS:":PRINT
135 PRINT 1,"INITIALIZE FILE"
140 PRINT 2,"CREATE A NEW ENTRY"
150 PRINT 3,"DISPLAY INVENTORY FOR ONE PART"
160 PRINT 4,"ADD TO STOCK"
170 PRINT 5,"SUBTRACT FROM STOCK"
180 PRINT 6,"DISPLAY ALL ITEMS BELOW REORDER LEVEL"
220 PRINT:PRINT:INPUT"FUNCTION";FUNCTION
225 IF (FUNCTION<1)OR(FUNCTION>6) THEN PRINT "BAD FUNCTION NUMBER":GOTO 130
230 ON FUNCTION GOSUB 900,250,390,480,560,680
240 GOTO 220
250 REM BUILD NEW ENTRY
260 GOSUB 840
270 IF ASC(F$) < > 255 THEN INPUT"OVERWRITE";A$: IF A$ < > "Y" THEN RETURN
280 LSET F$=CHR$(0)
290 INPUT "DESCRIPTION";DESC$
300 LSET D$=DESC$
310 INPUT "QUANTITY IN STOCK";Q%
320 LSET Q$=MKI$(Q%)
330 INPUT "REORDER LEVEL";R%
340 LSET R$=MKI$(R%)
350 INPUT "UNIT PRICE";P
360 LSET P$=MKS$(P)
370 PUT#1,PART%
380 RETURN
390 REM DISPLAY ENTRY
400 GOSUB 840
410 IF ASC(F$)=255 THEN PRINT "NULL ENTRY":RETURN
420 PRINT USING "PART NUMBER ###";PART%
430 PRINT D$
440 PRINT USING "QUANTITY ON HAND #####";CVI(Q$)
450 PRINT USING "REORDER LEVEL #####";CVI(R$)
460 PRINT USING "UNIT PRICE $$##.##";CVS(P$)
470 RETURN
480 REM ADD TO STOCK
490 GOSUB 840
500 IF ASC(F$)=255 THEN PRINT "NULL ENTRY":RETURN
510 PRINT D$:INPUT "QUANTITY TO ADD";A%
520 Q%=CVI(Q$)+A%
530 LSET Q$=MKI$(Q%)
540 PUT#1,PART%
550 RETURN
560 REM REMOVE FROM STOCK
570 GOSUB 840
580 IF ASC(F$)=255 THEN PRINT "NULL ENTRY":RETURN
590 PRINT D$
600 INPUT "QUANTITY TO SUBTRACT";S%
610 Q%=CVI(Q$)
620 IF (Q%-S%)<0 THEN PRINT "ONLY";Q%;"IN STOCK" :GOTO 600
630 Q%=Q%-S%
640 IF Q%= < CVI(R$) THEN PRINT "QUANTITY NOW";Q%;"REORDER LEVEL";CVI(R$)
650 LSET Q$=MKI$(Q%)
660 PUT#1,PART%
670 RETURN
680 REM DISPLAY ITEMS BELOW REORDER LEVEL4
690 FOR I=1 TO 100
710 GET#1,I
720 IF CVI(Q$)<CVI(R$) THEN PRINT D$;"QUANTITY";CVI(Q$) TAB(50) "REORDER LEVEL";CVI(R$)
730 NEXT I
740 RETURN
840 INPUT "PART NUMBER";PART%
850 IF(PART% < 1)OR(PART% > 100) THEN PRINT "BAD PART NUMBER":GOTO 840 ELSE GET#1,PART%:RETURN
890 END
900 REM INITIALIZE FILE
910 INPUT "ARE YOU SURE";B$:IF B$ < > "Y" THEN RETURN
920 LSET F$=CHR$(255)
930 FOR I=1 TO 100
940 PUT#1,I
950 NEXT I
960 RETURN

Constants, Variables, Expressions



Chapter 6
Constants, Variables, Expressions and Operators



After you have learned the fundamentals of programming in GW-BASIC, you will find that you will want to write more complex programs. The information in this chapter will help you learn more about the use of constants, variables, expressions, and operators in GW-BASIC, and how they can be used to develop more sophisticated programs.



6.1 Constants


Constants are static values the GW-BASIC Interpreter uses during execution of your program. There are two types of constants: string and numeric.


A string constant is a sequence of 0 to 255 alphanumeric characters enclosed in double quotation marks. The following are sample string constants:


HELLO

$25,000.00

Number of Employees

Numeric constants can be positive or negative. When entering a numeric constant in GW-BASIC, you should not type the commas. For instance, if the number 10,000 were to be entered as a constant, it would be typed as 10000. There are five types of numeric constants: integer, fixed-point, floating-point, hexadecimal, and octal.



























Constant Description
Integer Whole numbers between -32768 and +32767.  They do not contain decimal points.
Fixed-Point Positive or negative real numbers that contain decimal points.
Floating-Point Constants

Positive or negative numbers represented in exponential form (similar to scientific notation).  A floating-point constant consists of an optionally-signed integer or fixed-point number (the mantissa), followed by the letter E and an optionally-signed integer (the exponent). The allowable range for floating-point constants is 3.0×10-39 to 1.7×1038. For example:


235.988E-7=.0000235988
2359E6=2359000000
Hexadecimal

Hexadecimal numbers with prefix &H. For example:


&H76

&H32F
Octal

Octal numbers with the prefix &O or &. For
example:


&O347

&1234


6.1.1 Single- and Double-Precision Form for Numeric Constants


Numeric constants can be integers, single-precision, or double-precision numbers. Integer constants are stored as whole numbers only. Single-precision numeric constants are stored with 7 digits (although only 6 may be accurate). Double-precision numeric constants are stored with 17 digits of precision, and printed with as many as 16 digits.


A single-precision constant is any numeric constant with either



  • Seven or fewer digits

  • Exponential form using E

  • A trailing exclamation point (!)


A double-precision constant is any numeric constant with either



  • Eight or more digits

  • Exponential form using D

  • A trailing number sign (#)


The following are examples of single- and double-precision numeric constants:








Single-Precision ConstantsDouble-Precision Constants
46.8345692811
-1.09E-06-1.09432D-06
3489.03490.0#
22.5!7654321.1234


6.2 Variables


Variables are the names that you have chosen to represent values used in a GW-BASIC program. The value of a variable may be assigned specifically, or may be the result of calculations in your program. If a variable is assigned no value, GW-BASIC assumes the variable's value to be zero.



6.2.1 Variable Names and Declarations


GW-BASIC variable names may be any length; up to 40 characters are significant. The characters allowed in a variable name are letters, numbers, and the decimal point. The first character in the variable name must be a letter. Special type declaration characters are also allowed.


Reserved words (all the words used as GW-BASIC commands, statements, functions, and operators) can't be used as variable names. However, if the reserved word is embedded within the variable name, it will be allowed.  


Variables may represent either numeric values or strings.



6.2.2 Type Declaration Characters


Type declaration characters indicate what a variable represents. The following type declaration characters are recognized:








CharacterType of Variable
$String variable
%Integer variable
!Single-precision variable
#Double-precision variable

The following are sample variable names for each type:








Variable Type Sample Name
String variableN$
Integer variableLIMIT%
Single-precision variableMINIMUM!
Double-precision variablePl#

The default type for a numeric variable name is single-precision. Double-precision, while very accurate, uses more memory space and more calculation time. Single-precision is sufficiently accurate for most applications. However, the seventh significant digit (if printed) will not always be accurate. You should be very careful when making conversions between integer, single-precision, and double-precision variables.


The following variable is a single-precision value by default:


ABC

Variables beginning with FN are assumed to be calls to a user-defined function.


The GW-BASIC statements DEFINT, DEFSTR, DEFSNG, and DEFDBL may be included in a program to declare the types of values for certain variable names.



6.2.3 Array Variables


An array is a group or table of values referenced by the same variable name. Each element in an array is referenced by an array variable that is a subscripted integer or an integer expression. The subscript is enclosed within parentheses.  An array variable name has as many subscripts as there are dimensions in the array.


For example,


V(10)

references a value in a one-dimensional array, while


T(1,4)

references a value in a two-dimensional array.


The maximum number of dimensions for an array in GW-BASIC is
255. The maximum number of elements per dimension is 32767.





Note


If you are using an array with a subscript value greater than 10, you should use the DIM statement. Refer to the GW-BASIC User's Reference for more information. If a subscript greater than the maximum specified is used, you will receive the error message "Subscript out of range."





Multidimensional arrays (more than one subscript separated by commas) are useful for storing tabular data. For example, A(1,4) could be used to represent a two-row, five-column array such as the following:


Column     0    1    2    3    4
Row 0     10   20   30   40   50
Row 1     60   70   80   90  100

In this example, element A(1,2)=80 and A(0,3)=40.


Rows and columns begin with 0, not 1, unless otherwise declared. For more information, see the OPTION BASE statement in the GW-BASIC User's Reference.



6.2.4 Memory Space Requirements for Variable Storage


The different types of variables require different amounts of storage. Depending on the storage and memory capacity of your computer and the size of the program that you are developing, these can be important considerations.












VariableRequired Bytes of Storage
Integer2
Single-precision4
Double-precision8
  
ArraysRequired Bytes of Storage
Integer2 per element
Single-precision4 per element
Double-precision8 per element


Strings:


Three bytes overhead, plus the present contents of the string as one byte for each character in the string. Quotation marks marking the beginning and end of each string are not counted.



6.3 Type Conversion


When necessary, GW-BASIC converts a numeric constant from one type of variable to another, according to the following rules:



  • If a numeric constant of one type is set equal to a numeric variable of a different type, the number is stored as the type declared in the variable name.

    For example:


    10 A% = 23.42
    20 PRINT A%
    RUN
     23

    If a string variable is set equal to a numeric value or vice versa, a "Type Mismatch" error occurs.



  • During an expression evaluation, all of the operands in an arithmetic or relational operation are converted to the same degree of precision; that is, that of the most precise operand. Also, the result of an arithmetic operation is returned to this degree of precision. For example:


    10 D# = 6#/7
    20 PRINT D#
    RUN
    .8571428571428571

    The arithmetic is performed in double-precision, and the result is returned in D# as a double-precision value.


    10 D = 6#/7
    20 PRINT D
    RUN
    .8571429


    The arithmetic is performed in double-precision, and the result is returned to D (single-precision variable) rounded and printed as a single-precision value.



  • Logical operators convert their operands to integers and return an integer result. Operands must be within the range of -32768 to 32767 or an
    "Overflow" error occurs.

  • When a floating-point value is converted to an integer, the fractional portion is rounded. For example:


    10 C% = 55.88
    20 PRINT C%
    RUN
     56


  • If a double-precision variable is assigned a single-precision value, only the first seven digits (rounded), of the converted number are valid. This is because only seven digits of accuracy were supplied with the single-precision value. The absolute value of the difference between the printed double-precision number, and the original single-precision value, is less than 6.3E-8 times the original single-precision value. For example:


    10 A = 2.04
    20 B# = A
    30 PRINT A;B#
    RUN
     2.04 2.039999961853027






6.4 Expressions and Operators


An expression may be simply a string or numeric constant, a variable, or it may combine constants and variables with operators to produce a single value.


Operators perform mathematical or logical operations on values. The operators provided by GW-BASIC are divided into four categories:



  • Arithmetic

  • Relational

  • Logical

  • Functional



6.4.1 Arithmetic Operators


The following are the arithmetic operators recognized by GW-BASIC. They appear in order of precedence.










OperatorOperation
^Exponentiation
-Negation
*Multiplication
/Floating-point Division
+Addition
-Subtraction

Operations within parentheses are performed first. Inside the parentheses, the usual order of precedence is maintained.


The following are sample algebraic expressions and their GW-BASIC counterparts:










Algebraic ExpressionBASIC Expression
X–Z/Y(X-Y)/Z
XY/ZX*Y/Z
X+Y/Z(X+Y)/Z
(X2)Y(X^2)^Y
XYZX^(Y^Z)
X(-Y)X*(-Y)

Two consecutive operators must be separated by parentheses.



6.4.1.1 Integer Division and Modulus Arithmetic


Two additional arithmetic operators are available: integer division and modulus arithmetic.


Integer division is denoted by the backslash (\). The operands are rounded to integers (must be within the range of -32768 to 32767) before the division is performed, and the quotient is truncated to an integer.


The following are examples of integer division:


10\4 = 2

25.68\6.99 = 3

In the order of occurrence within GW-BASIC, the integer division will be performed just after floating-point division.


Modulus arithmetic is denoted by the operator MOD. It gives the integer value that is the remainder of an integer division.


The following are examples of modulus arithmetic:


10.4 MOD 4 = 2
(10/4=2 with a remainder 2)

25.68 MOD 6.99 = 5
(26/7=3 with a remainder 5)

In the order of occurrence within GW-BASIC, modulus arithmetic follows integer division. The INT and FIX functions, described in the GW-BASIC User's Reference, are also useful in modulus arithmetic.



6.4.1.2 Overflow and Division by Zero


If, during the evaluation of an expression, a division by zero is encountered, the "Division by zero" error message appears, machine infinity with the sign of the numerator is supplied as the result of the division, and execution continues.


If the evaluation of an exponentiation results in zero being raised to a negative power, the "Division by zero" error message appears, positive machine infinity is supplied as the result of the exponentiation, and execution continues.


If overflow occurs, the "Overflow" error message appears, machine infinity with the algebraically correct sign is supplied as the result, and execution continues. The errors that occur in overflow and division by zero will not be trapped by the error trapping function.



6.4.2 Relational Operators


Relational operators let you compare two values. The result of the comparison is either true (-1) or false (0). This result can then be used to make a decision regarding program flow.


Table 6.1 displays the relational operators.



Table 6.1


Relational Operators










OperatorRelation TestedExpression
=EqualityX=Y
<>InequalityX<>Y
<Less thanX<Y
>Greater thanX>Y
<=Less than or equal toX<=Y
>=Greater than or equal toX>=Y

The equal sign is also used to assign a value to a variable. See the LET statement in the GW-BASIC User's Reference.


When arithmetic and relational operators are combined in one expression, the arithmetic is always performed first:


X+Y < (T-1)/Z

This expression is true if the value of X plus Y is lessthan the value of T-1 divided by Z.



6.4.3 Logical Operators


Logical operators perform tests on multiple relations, bit manipulation, or Boolean operations. The logical operator returns a bit-wise result which is either true (not zero) or false (zero). In an expression, logical operations are performed after arithmetic and relational operations. The outcome of a logical operation is determined as shown in the following table. The operators are listed in order of precedence.



Table 6.2


Results Returned by Logical Operations





































Operation Value Value Result
NOTX NOT X
T F
F T
ANDXYX  AND  Y
TTT
TFF
FTF
FFF
ORXYX  OR  Y
TTT
TFT
FTT
FFF
XORXYX  XOR  Y
TTF
TFT
FTT
FFF
EQVXYX  EQV  Y
TTT
TFF
FTF
FFT
IMPXYX  IMP  Y
TTT
TFF
FTT
FFT

Just as the relational operators can be used to make decisions regarding program flow, logical operators can connect two or more relations and return a true or false value to be used in a decision. For example:


IF D<200 AND F<4 THEN 80

IF I>10 OR K<0 THEN 50

IF NOT P THEN 100

Logical operators convert their operands to 16-bit, signed, two's complement integers within the range of -32768 to +32767 (if the operands are not within this range, an error results). If both operands are supplied as 0 or -1, logical operators return 0 or -1. The given operation is performed on these integers in bits; that is, each bit of the result is determined by the corresponding bits in the two operands.


Thus, it is possible to use logical operators to test bytes for a particular bit pattern. For instance, the AND operator may be used to mask all but one of the bits of a status byte at a machine I/O port. The OR operator may be used to merge two bytes to create a particular binary value. The following examples demonstrate how the logical operators work:































ExampleExplanation
63 AND 16=16 63 = binary 111111 and 16 = binary 10000, so 63 AND 16 = 16
15 AND 14=14 15 = binary 1111 and 14 = binary 1110, so 15 AND 14 = 14 (binary 1110)
-1 AND 8=8 -1 = binary 1111111111111111 and 8 = binary 1000, so -1 AND 8 = 8
4 OR 2=6 4 = binary 100 and 2 = binary 10, so 4 OR 2 = 6 (binary 110)
10 OR 10=10 10 = binary 1010, so 1010 OR 1010 =1010 (10)
-1 OR -2=-1 -1 = binary 1111111111111111 and -2 = binary 1111111111111110, so -1 OR -2 = -1. The bit complement of 16 zeros is 16 ones, which is the two's complement representation of -1.
NOT X=-(X+1) The two's complement of any integer is the bit complement plus one.


6.4.4 Functional Operators


A function is used in an expression to call a predetermined operation that is to be performed on an operand. GW-BASIC has intrinsic functions that reside in the system, such as SQR (square root) or SIN (sine).


GW-BASIC also allows user-defined functions written by the programmer. See the DEF FN statement in the GW-BASIC User's Reference.



6.4.5 String Operators


To compare strings, use the same relational operators used with numbers:










OperatorMeaning
=Equal to
<>Unequal
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to

The GW-BASIC Interpreter compares strings by taking one character at a time from each string and comparing their ASCII codes. If the ASCII codes in each string are the same, the strings are equal. If the ASCII codes differ, the lower code number will precede the higher code. If the interpreter reaches the end of one string during string comparison, the shorter string is said to be smaller, providing that both strings are the same up to that point. Leading and trailing blanks are significant.


For example:


"AA" < "AB"

"FILENAME" = "FILENAME"

"X&" > "X#"

"CL " > "CL"

"kg" > "KG"

"SMYTH" < "SMYTHE"

B$ < "9/12/78" where B$ = "8/12/78"

String comparisons can also be used to test string values or to alphabetize strings. All string constants used in comparison expressions must be enclosed in quotation marks.


Strings can be concatenated by using the plus (+) sign. For example:


10 A$="FILE":B$="NAME"
20 PRINT A$+B$
30 PRINT "NEW " + A$+B$
RUN
FILENAME
NEW FILENAME

Error Codes and Messages



Error Codes and Messages









































































































































































































































CodeMessage
1

NEXT without FOR


NEXT statement does not have a corresponding FOR statement. Check variable at FOR statement for a match with the NEXT statement variable.
2

Syntax error


A line is encountered that contains an incorrect sequence of characters (such as unmatched parentheses, a misspelled command or statement, incorrect punctuation). This error causes GW-BASIC to display the incorrect line in edit mode.
3

RETURN without GOSUB


A RETURN statement is encountered for which there is no previous GOSUB statement.
4

Out of DATA


A READ statement is executed when there are no DATA statements with unread data remaining in the program.
5

Illegal function call


An out-of-range parameter is passed to a math or string function. An illegal function call error may also occur as the result of:

  • a negative or unreasonably large subscript

  • a negative or zero argument with LOG

  • a negative argument to SQR

  • a negative mantissa with a noninteger power

  • a call to a USR function for which the starting address has not yet been given

  • an improper argument to MID$, LEFT$, RIGHT$, INP, OUT, WAIT, PEEK, POKE, TAB, SPC, STRING$, SPACE$, INSTR, or ON...GOTO


6

Overflow


The result of a calculation is too large to be represented in GW-BASIC's number format. If underflow occurs, the result is zero, and execution continues without an error.
7

Out of memory


A program is too large, has too many FOR loops, GOSUBs, variables, or expressions that are too complicated. Use the CLEAR statement to set aside more stack space or memory area.
8

Undefined line number


A line reference in a GOTO, GOSUB, IF-THEN...ELSE, or DELETE is a nonexistent line.
9

Subscript out of range


An array element is referenced either with a subscript that is outside the dimensions of the array, or with the wrong number of subscripts.
10

Duplicate Definition


Two DIM statements are given for the same array, or a DIM statement is given for an array after the default dimension of 10 has been established for that array.
11

Division by zero


A division by zero is encountered in an expression, or the operation of involution results in zero being raised to a negative power. Machine infinity with the sign of the numerator is supplied as the result of the division, or positive machine infinity is supplied as the result of the involution, and execution continues.
12

Illegal direct


A statement that is illegal in direct mode is entered as a direct mode command.
13

Type mismatch


A string variable name is assigned a numeric value or vice versa; a function that expects a numeric argument is given a string argument or vice versa.
14

Out of string space


String variables have caused GW-BASIC to exceed the amount of free memory remaining. GW-BASIC allocates string space dynamically until it runs out of memory.
15

String too long


An attempt is made to create a string more than 255 characters long.


16

String formula too complex


A string expression is too long or too complex. Break the expression into smaller expressions.


17

Can't continue


An attempt is made to continue a program that



  • Has halted because of an error

  • Has been modified during a break in execution

  • Does not exist


18

Undefined user function


A USR function is called before the function definition (DEF statement) is given.


19

No RESUME


An error-trapping routine is entered but contains no RESUME statement.


20

RESUME without error


A RESUME statement is encountered before an error-trapping routine is entered.


21

Unprintable error


No error message is available for the existing error condition. This is usually caused by an error with an undefined error code.


22

Missing operand


An expression contains an operator with no operand following it.


23

Line buffer overflow


An attempt is made to input a line that has too many characters.


24

Device Timeout


GW-BASIC did not receive information from an I/O device within a predetermined amount of time.


25

Device Fault


Indicates a hardware error in the printer or interface card.


26

FOR Without NEXT


A FOR was encountered without a matching NEXT.


27

Out of Paper


The printer is out of paper; or, a printer fault.


28

Unprintable error


No error message is available for the existing error condition. This is usually caused by an error with an undefined error code.


29

WHILE without WEND


A WHILE statement does not have a matching WEND.


30

WEND without WHILE


A WEND was encountered without a matching WHILE.


31-49

Unprintable error


No error message is available for the existing error condition. This is usually caused by an error with an undefined error code.


50

FIELD overflow


A FIELD statement is attempting to allocate more bytes than were specified for the record length of a random file.


51

Internal error


An internal malfunction has occurred in GW-BASIC. Report to your dealer the conditions under which the message appeared.


52

Bad file number


A statement or command references a file with a file number that is not open or is out of range of file numbers specified at initialization.


53

File not found


A LOAD, KILL, NAME, FILES, or OPEN statement references a file that does not exist on the current diskette.


54

Bad file mode


An attempt is made to use PUT, GET, or LOF with a sequential file, to LOAD a random file, or to execute an OPEN with a file mode other than I, O, A, or R.


55

File already open


A sequential output mode OPEN is issued for a file that is already open, or a KILL is given for a file that is open.


56

Unprintable error


An error message is not available for the error condition which exists. This is usually caused by an error with an undefined error code.


57

Device I/O Error


Usually a disk I/O error, but generalized to include all I/O devices. It is a fatal error; that is, the operating system cannot recover from the error.


58

File already exists


The filename specified in a NAME statement is identical to a filename already in use on the diskette.


59-60

Unprintable error


No error message is available for the existing error condition. This is usually caused by an error with an undefined error code.


61

Disk full


All disk storage space is in use.


62

Input past end


An INPUT statement is executed after all the data in the file has been input, or for a null (empty) file. To avoid this error, use the EOF function to detect the end of file.


63

Bad record number


In a PUT or GET statement, the record number is either greater than the maximum allowed (16,777,215) or equal to zero.


64

Bad filename


An illegal form is used for the filename with LOAD, SAVE, KILL, or OPEN; for example, a filename with too many characters.


65

Unprintable error


No error message is available for the existing error condition. This is usually caused by an error with an undefined error code.


66

Direct statement in file


A direct statement is encountered while loading a ASCII-format file. The LOAD is terminated.


67

Too many files


An attempt is made to create a new file (using SAVE or OPEN) when all directory entries are full or the file specifications are invalid.


68

Device Unavailable


An attempt is made to open a file to a nonexistent device. It may be that hardware does not exist to support the device, such as lpt2: or lpt3:, or is disabled by the user. This occurs if an OPEN "COM1: statement is executed but the user disables RS-232 support with the /c: switch directive on the command line.


69

Communication buffer overflow


Occurs when a communications input statement is executed, but the input queue is already full. Use an ON ERROR GOTO statement to retry the input when this condition occurs. Subsequent inputs attempt to clear this fault unless characters continue to be received faster than the program can process them. In this case several options are available:



  • Increase the size of the COM receive buffer with the /c: switch.

  • Implement a hand-shaking protocol with the host/satellite (such as: XON/XOFF, as demonstrated in the TTY programming example) to turn transmit off long enough to catch up.

  • Use a lower baud rate for transmit and receive.


70

Permission Denied


This is one of three hard disk errors returned from the diskette controller.



  • An attempt has been made to write onto a diskette that is write protected.

  • Another process has attempted to access a file already in use.

  • The UNLOCK range specified does not match the preceding LOCK statement.


71

Disk not Ready


Occurs when the diskette drive door is open or a diskette is not in the drive. Use an ON ERROR GOTO statement to recover.


72

Disk media error


Occurs when the diskette controller detects a hardware or media fault. This usually indicates damaged media. Copy any existing files to a new diskette, and reformat the damaged diskette. FORMAT maps the bad tracks in the file allocation table. The remainder of the diskette is now usable.


73

Advanced Feature


An attempt was made to use a reserved word that is not available in this version of GW-BASIC.


74

Rename across disks


Occurs when an attempt is made to rename a file to a new name declared to be on a disk other than the disk specified for the old name. The naming operation is not performed.


75

Path/File Access Error


During an OPEN, MKDIR, CHDIR, or RMDIR operation, MS-DOS is unable to make a correct path-to-filename connection. The operation is not completed.


76

Path not found


During an OPEN, MKDIR, CHDIR, or RMDIR operation, MS-DOS is unable to find the path specified. The operation is not completed.