Gw Basic Programming Language
- Download Gw Basic Programming Language
- Gw Basic Programming Examples
- Gw Basic Download Programming Language
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.
Programming Language (GW-BASIC)Gee-Whiz Beginners All Purpose Symbolic Instructions Code.
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.
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:. Turn on your computer. Insert your working copy of the MS-DOS diskette into Drive A of your computer, and press RETURN. 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.
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 statementstatements 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. 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 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. The numbered lines. 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. 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:.
Press the F4 key. The command word SAVE' appears on your screen. 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:. Press the F3 key.
The command load LOAD' appears on your screen. Type the name of the file. Press RETURN. The file is loaded into memory, and ready for you to list, edit, or run. 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:. Open the file in output ( O) mode. The current program will use this file first for output: OPEN 'O',#1,' filename'. Write data to the file using the PRINT# or WRITE# statement: PRINT#1,A$ PRINT#1,B$ PRINT#1,C$.
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. 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 2 32 -1. The following statements and functions are used with random files: CLOSE FIELD MKI$ CVD LOC MKS$ CVI LOCK OPEN CVS LOF PUT EOF LSET/RSET UNLOCK ET MKD$ 5.3.1 Creating a Random Access File The following program steps are required to create a random data file:. 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. 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$. 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$. 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.
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×10 38. For example: 235.988E-7=. 2359E6= 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 Constants Double-Precision Constants 46.8 345692811 -1.09E-06 -1.09432D-06 3489.0 3490.0# 22.5! 76 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: Character Type 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 variable N$ Integer variable LIMIT% Single-precision variable MINIMUM! Double-precision variable Pl# 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 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.
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. Variable Required Bytes of Storage Integer 2 Single-precision 4 Double-precision 8 Arrays Required Bytes of Storage Integer 2 per element Single-precision 4 per element Double-precision 8 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.428571 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.53027 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. Vigo software. Functional 6.4.1 Arithmetic Operators The following are the arithmetic operators recognized by GW-BASIC. They appear in order of precedence.
Operator Operation ^ 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 Expression BASIC Expression X–Z/Y (X-Y)/Z XY/Z X.Y/Z X+Y/Z (X+Y)/Z (X 2) Y (X^2)^Y X Y Z X^(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, 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 Operator Relation Tested Expression = Equality X=Y Inequality XY Greater than XY = Greater than or equal to X=Y The equal sign is also used to assign a value to a variable. See the LET statement in the.
When arithmetic and relational operators are combined in one expression, the arithmetic is always performed first: X+Y 10 OR K Unequal Greater than = 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' 'X#' 'CL ' 'CL' 'kg' 'KG' 'SMYTH'.
Error Codes and Messages Code Message 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.
Download Gw Basic Programming Language
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.
Gw Basic Programming Examples
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.
Gw Basic Download Programming Language
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.
3.23 / 1988; 30 years ago ( 1988) Influenced by, Influenced, GW-BASIC is a dialect of the developed by from, originally for. It is otherwise identical to Microsoft/, but is a fully self-contained executable and does not need the BASIC. It was bundled with on by Microsoft.
Microsoft also sold a BASIC, BASCOM, compatible with GW-BASIC, for programs needing more speed. The language is suitable for simple games, business programs and the like. Since it was included with most versions of MS-DOS, it was also a low-cost way for many aspiring to learn the fundamentals of. With the release of MS-DOS 5.0, GW-BASIC's place was eventually taken by, the interpreter part of the separately available compiler. Contents. Features IBM BASICA and GW-BASIC are direct ports of Microsoft's BASIC-80 (also known as MBASIC) designed for 8080/Z80 machines, but with added features specifically for the IBM PC hardware. 'KindlyRat'.
Archived from on 2005-07-26. Retrieved 2009-11-10. Archived from on 2009-10-26.
Retrieved 2009-11-10. Retrieved 2008-06-12. Archived from on 2007-12-17. Retrieved 2008-06-28. ^ Gregory Whitten (2005-04-13). Archived from on 2008-09-20.
Retrieved 2008-06-29. Retrieved 2008-06-28.
Archived from on 2008-06-29. Retrieved 2008-06-28.
External links., a resource for BASIC games and other programs., another BASIC resource site. at the (archived October 27, 2009). regarding BASIC, with timeline dates for DOS, Windows and BASIC dialects. Neil C.
Obremski's site devoted to GW-BASIC. a GW-BASIC emulator for modern operating systems.