Digital PDFs
Documents
Guest
Register
Log In
XX-91249-B4
1973
147 pages
Original
8.8MB
view
download
Document:
EDUCOMP BASIC Users Manual 19
Order Number:
XX-91249-B4
Revision:
Pages:
147
Original Filename:
http://bitsavers.org/pdf/dec/pdp8/basic/EDUCOMP_BASIC_Users_Manual_1973.pdf
OCR Text
u o A c USE'S ANU EDUCOMP CORPORATION 298 PARK ROAD WEST HARTFORD) CONNECTIC UT Copyright (§) 1973 EDUCOMP Corporation TABLE OF CONTENTS 1. AN OVERVIEW 1.1 1.2 EDUCOMP BASIC IS ITSELF A PROGRAM 1-1 BASIC AS A PROGRAMMING LANGUAGE 1-2 1.2.1 Statements and Commands 1-2 1.2.2 Running BASIC 1-3 1.3 THE ENVIRONMENT 1-4 1.4 CONVENTIONS USED IN THIS MANUAL 1-4 1.5 SPECIAL TERMINAL KEYS 1-6 2. FUNDAMENTALS OF EDUCOMP RASIC 2.1 SAMPLE BASIC PROGRAM 2-1 2.2 LINE NUMBERS 2-1 2.3 STATEMENTS 2-3 2.3.1 2-3 Multiple Statements on a Single Line 2.3.2 2-3 2.4 Continuation of a Single Statement onto Another Line CHARACTER SET 2.5 EXPRESSIONS 2-4 2.5.1 Numbers 2-5 2.5.2 Variables 2-6 2.5.3 Mathematical Operators 2-7 2.5.4 Relational Symbols 2-8 2-4 3. ELEMENTARY BASIC STATEMENTS .. 3.1 LET STATEMENT 3-1 . 3.2 PROGRAMMED INPUT AND OUTPUT 3-2 3.3 UNCONDITIONAL BRANCH, GOTO STATEMENT 3-5 3.4 CONDITIONAL BRANCH, IF-THEN STATEMENT 3-6 ~.4.1 3-9 3.5 Logical IF-THEN PROGRAM LOOPS 3-10 3.5.1 FOR and NEXT Statements 3-12 3.5.2 Subscripted Variables and the DIM Statement 3-15 3.6 MATHEMATICAL FUNCTIONS 3.6.1 3.6.2 3.7 3-'18 Examples of Particular Intrinsic Functions 3-19 RANDOMIZE Statement 3-22 SUBROUTINES 3-23 3.7.1 GOSUB Statement 3-25 3.7.2 RETURN Statement 3-25 3.7.3 Nesting Subroutines 3-25 3.8 STOP AND END STATEMENTS 3-26 3.9 REMARKS AND COMMENTS 3-27 3.10 ON-GOTO STATEMENT 3-28 3.11 ON-GOSUB STATEMENT 3-28 4. CHARACTER STRINGS 4.1 CHARACTER STRINGS. 4-1 4.1.1 String Constants 4-2 4.1.2 Character String Variables 4-2 4.1.3 Subscripted String Variables 4-3 4.1.4 String Size 4-4 4.1.5 Relational Operators 4-5 4.2 STRING INPUT 4-7 4.3 STRING OUTPUT 4-9 5. DATA STORAGE CAPABILITIES 5.1 FILE STORAGE 5-1 5.2 OPEN STATEMENT 5-2 5.2.1 Formatted ASCII I/O 5-3 5.2.2 File-Structured Vs. Non-FileStructured Devices 5-4 Opening the User Terminal as an I/O Channel 5-6 5.2.3 5.3 OUTPUT TO VARIOUS DEVICES 5-7 5.4 INPUT FROM VARIOUS DEVICES 5-9 5.5 VIRTUAL DATA STORAGE 5-10 5.5.1 Virtual Core DIM Statement 5-l0 5.5.2 Virtual Core String Storage 5-11 5.5.3 Opening a Virtual Core File 5-12 5.6 CLOSE STATEMENT 5-13 5.7 KILL STATEMENT 5-14 5.8 CHAIN STATEMENT 5-14 6. EDUBASIC GENERALIZED INPUT AND OUTPUT OPERATIONS 6.1 READ AND DATA STATEMENTS 6-1 6.2 RESTORE STATEMENT 6-2 6.3 INPUT STATEMENT 6-2 6.4 PRINT STATEMENT 6-4 6.4.1 PRINT-USING Statement 6-6 6.4.2 PRINT Functions 6-9 7i EDUBASIC COMMANDS' 7.1 INTRODUCTION 7-1 7.2 7.3 CREATING A PROGRAM CALLING AN EXISTING PROGRAM 7-1 7.3.1 Calling Data Files 7-4 7.3.2 OVerlaying A Program 7-5 7.4 7.5 7-3 EDITING PROGRAMS 7-6 7.4.1 The EDIT Command 7-7 7.4.2 The RESEQUENCE Command 7-8 7.4.3 DELETE Command 7-9 7.4.4 LIST Command 7-10 7.4.5 SEARCH MANIPULATING USER PROGRAMS 7-11 7';"12 7.5.1 RUN Command 7-12 7.5.2 EXECUTE Command 7-13 7.5.3 SAVE Command 7-14 7.5.4 SAVE Without Line Numbers 7-16 7.5.5 UNSAVE Command 7-16 7.5.6 RENAME Command 7-17 7.5.7 REPLACE and NREPLACE 7-17 7.5.8 COMPILE Command 7-17 7.6 LENGTH COMMAND 7-19 7. 7 CATALOG COMMAND 7-19 7.8 7.9 COMMANDS FOR INPUT/OUTPUT DEVICES 7.8.1 TAPE Command 7.8.2 KEY Command 7.8.3 PUNCH and NPUNCH 7.8.4 MARGIN Command SPECIAL CONTROL CHARACTERS 7.9.1 RETURN Key 7.9.2 LINE FEED Key 7-22 7-22 7-22 7-23 7.9.3 7.9.4 7.9.5 RUB OUT Key CTRL/C CTRL/P 7-23 7-24 7-24 7.9.6 7.9.7 CTRL/U CTRL/O TAB Character CTRL/Z 7-24 7-25 7-25 7-25 7.9.8 7.9.9 7-20 7-20 7-21 7-21 8. DETAILS OF VIRTUAL ARRAYS 8.1 8.2 INTRODUCTION ARRAY STORAGE 8-1 8-4 8.3 TRANSLATION OF ARRAY SUBSCRIPTS INTO FILE ADDRESSES 8-4 ACCESS TO DATA IN VIRTUAL ARRAYS ·8-8 8.4 CHAPTER 1 AN OVERVIEW 1.1 EDUCOMP BASIC IS ITSELF A PROGRAM The computer language called BASIC is, itself, a computer program. It is a very complex program written in a dif- ferent kind of comput~r language ("machine" language), one that requires great detail when writing a program. Use of this great detail permits the construction of the language BASIC, which will let you write programs using a few English words and mathematical symbols. Because BASIC itself is a computer program, it has a limited number of ways of accepting instructions (statements and commands). Thus, the major effort in learning the language must be to master the rules for giving instructions and learning what the instructions do for you. (The error message uILLEGAL SYNTAX" may be somewhat disconcerting in a computer-oriented endeavor but BASIC is a "language".l Historically, the original development of BASIC was done at Dartmouth College by Drs. John Kemeny and Thomas Kurtz and it may properly be considered to have a bias toward an educational environment. (Dr. Kemeny was later made President of Dartmouth College.) BASIC is an acronym for Beginners All-purpose Symbolic Instruction Code. The objective in de- veloping BASIC was to provide an easily learned computer language that would run on a computer in an interactive mode. The plan was to bring the computer to the student and provide instant service by means of terminals placed around the campus rather than require the student to go to the computer and face a lengthy wait to get his program run. Although BASIC has been considerably extended, and several versions exist, the few fundamental instructions are common to all 1-1 versions and are easy to learn. 1.'2 . BASIC AS A PROGRAMMING LANGUAGE BASIC is one of the simplest of all programming languages because of its small number of powerful but self-explanatory statements and commands and its easy application in solving problems. Its wide use in scientific, business, and educa- tional installations attests to its value and straightforward application. BASIC is similar to many other programming languages in various respects (and is, consequently, very easy for the experienced programmer to learn), but is especially suited for time-sharing because of its conversational nature. A conver- sational language is one which allows the user to communicate with the language processor by typing on the terminal keyboard. BASIC responds by printing on the terminal printer, providing for an interactive man/machine relationship. EDUCOMP BASIC contains both elementary statements used to write simple programs and advanced programming techniques and statements to write complex and efficient programs. The key word here is not complex, but efficient. As the user progresses and gains programming experience, he will naturally find himself becoming more efficient and able to use the more sophisticated data manipulations. Almost any problem can be solved with the simple BASIC statements. Later in the user's programming experience, the advanced techniques can be added. 1.2.1 STATEMENTS AND COMMANDS Instructions in BASIC are one of two types: or (2) commands. 1-2 (1) statements Statements are the instructions which make up the user program. They give the details of how the program is to perform the Job to be done, i.e., solve your problem. Learning BASIC means learning what each statement does. As,you learn how to combine the various kinds of statements in different ways you will find that writing a program becomes progressively easier and, for many people, a fascinating activity much like solving a puzzle. a line number. In BASIC, each statement starts with Each statement also includes a key word in English which indicates what the statement is to do. Commands, in BASIC, are the instructions that tell BASIC how to act upon, or with, your program. Commands are external to the program which consists of statements. Because com- mands direct BASIC to work with the program, they are used in writing, editing, running, debugging and finally, storing your program. EDUCOMP BASIC is particularly rich in commands designed to make programming easier. command is RUN. An example of a BASIC RUN causes BASIC to start the sequence of events which, if the program is syntactically correct will result in execution of your program. Note that commands use no line numbers, and are executed immediately after they are typed. 1.2.2 RUNNING BASIC When the RUN command is given, BASIC attempts to complete a two-phase operation: (1) compilation and (2) execution. Compilation is the process by which BASIC translates the program coding you have written (in BASIC) into a form "understood" by the computer (machine language). The compiler checks the program to make certain that all the rules of the language are obeyed. When the statements are correct with respect to the syntax of BASIC, the conversion to machine language takes place and the second phase of running a program begins. EDUCOMP BASIC is an incremental compiler which partially compiles the 1-3 user's program. This partial compilation is to be compared with a pure interpreter which is much slower in running programs. Execution is the actual operation of the program to perform its assigned task (solve your problem). During execution the computer is performing the operations it was instructed to perform in the manner specified by the program. The computer can do only what it is instructed to do by the program! 1.3 THE ENVIRONMENT The environment under which EDUCOMP BASIC functions is an extremely complex arrangement of interrelated programs. This particular manual is designed only to convey the constructs of the EDUCOMP BASIC language and it is assumed that the reader is (or will become) familiar with Digital Equipment Corporation's operating system OS/8. We make the assumption here that the user is always in the language BASIC. Briefly, obtaining EDUBASIC requires (1) getting OS/8 on the air, i.e., starting the system, and (2) typing .R BASIC at the terminal in response to the dot " " printed by OS/8. EDUBASIC responds with READY to indicate that the system is in BASIC and 'READY' to perform for you. 1.4 CONVENTIONS USED IN THIS MANUAL Certain documentation conventions are used throughout this manual to clarify examples of BASIC syntax. 1-4 Each BASIC statement is described at least once in general terms using the following conventions: a. Words appearing in capital letters are the key words and indicate what the statement is expected to do with the data found after the key word, if any. For example: line number b. READ list Square brackets indicate that the bracketed item is optional. For example: line number [LET] variable = expression c. Items in lower case type (formula, variable, list, etc.) are supplied by the user according to rules explained in the text. Items in capital letters (END, IF, READ, etc.) must appear exactly as shown because they form the BASIC language. d. The term line number used in examples (as in (b) above) indicates that any line number is valid. The use of some terms in this document may be unfamiliar to the new user. The following definitions and explanations are valid throughout this manual: a. BASIC (that is, the computer) prints on the teleprinter whereas the user types on the keyboard. h. A statement is a line (or part of a line or multiple lines in some cases) within a user program containing a BASIC language instruction. Each line is terminated by typing the RETURN key. c. Commands cause BASIC to perform some operation or task immediately and are not preceded by a line number. Commands are always terminated with the RETURN key. d. User programs consist of a series of statements written by a person using the system in the BASIC language. e. The terminal is in most cases an ASR-33 Teletype 1 • However, we can accommodate virtually any typewriter type device. The user terminal is alternatively referred to as terminal, teleprinter, or keyboard, depending upon what part or whether the whole device is indicated. ITeletype is a registered trademark of the Teletype Corporation. 1-5 1.5 SPECIAL TERMINAL KEYS Throughout this manual, reference is made to typing various special keys on the terminal. In many cases, these keys are not mentioned, but assumed. The user will quickly learn the use of the more important control keys on the terminal. As an introduction, the user is directed to consider the keys explained below. The RETURN key causes two operations to be performed: a. An automatic carriage return/line feed operation is executed. The printing head returns to the beginning of the line (carriage return) and the paper is advanced one line (line feed). b. The data preceding the typing of the RETURN key is entered into the system for evaluation. All commands to BASIC and lines in a user program are terminated by typing the RETURN key. The RUBOUT key is used to correct typing mistakes. Typing this key once causes the last character typed to be deleted from the terminal input buffer (remember that an entire line is entered at once when the RETURN key is typed). Pressing the RUBOUT key N times causes the last N characters of the current line to be deleted. The CTRL key (or control key) is used in combination with certain letter keys to cause BASIC to perform special operations. These combinations are performed by the user holding down the CTRL key while typing the desired letter key, then releasing both keys. tions. CTRL/U and CTRL/C are examples of these combina- Some of the CTRL/key combinations are introduced , below for use when working through this manual. All usable combinations are described in Chapter 11. a. CTRL/U is used to delete an entire line up to the last point at which the RETURN or ESCAPE key was last typed. BASIC responds with a carriage return/line feed so that the user can continue typing 1-6 on a fresh line. b. CTRL/P is used to interrupt the execution of a program and return to the interact~ve BASIC processor. When typed by the user, CTRL/P causes the system to echo tP when BASIC is in command mode and the system prints READY. When used to interrupt the execution of a progrfu~, CTRL/P is not printed, but the message STOP IN LINE xxx followed by READY is printed. c. CTRL/C returns control of the terminal to the OS/8 monitor. The LINE FEED key reprints the current line, free of rubouts. It does not enter the line into the program. return is needed to perform that function. 1-7 The carriage CHAPTER 2 FUNDAMENTALS OF EDUCOMP BASIC 2.1 SAMPLE BASIC PROGRAM The program in Figure 2.1 is an example of a user program written in the BASIC language. It illustrates the syntax* and elements of the language as well as standard formatting of statements and the appearance of terminal output. The user program (the lines numbered 10 through 999) may at this time mean little, although the remark in the first line (line 5) and the printed results (following the word RUN) clearly show that the program performs payroll calculations. A user program is composed of lines of statements containing instructions to BASIC. Each line of the program begins with a line number that serves to identify that line as a statement and to indicate the order in which statements are to be evaluated for execution. Each statement starts with a 'key' word which specifies the type of operation to be performed • .2. 2 LINE NUMBERS Each line of a user program must be preceded by a line number. Line numbers have the following characteristics: 1. They indicate the order in which statements are normally evaluated; 2. The numbers serve as 'tags' to enable the normal order of evaluation to be changed; that is, the execution of the program can branch or loop through designated statements (this is explained further in the sections on the GOTO, GOSUB, and IF-THEN statements in Chapter 3); and 3. Line numbers enhance program editing by permitting modification of any specified line without affecting any other portion of the program. *Syntax refers to the rules which specify how the programming language elements are combined. 2-1 Line numbers. are in the range 1 to 4094. It is good programming practice to number lines in increments of 5 or 10 when first writing a program, to allow for insertion of forgotten or additional lines to complete the program. LISTNH 5 REMARK -PAYROLL CHECKSTUBS SIMULATION W/DATA US PRINT "COMPLETE PAYROLL DEMONSTRAT.ION": PRINT: PRINT 15 G=0:READ E 17 IF E=-999 THEN 999 20 READW"H"D"Y"V 22 PRINT "EMPLOYEE NUMBER"; E 24 PRIN T "HO URS tvO RKED="'; li 26 PRINT "HOURL Y WAGE="Hl: PRINT 30 0=0: IF- H<=40 THEN 60 35 LET T=H-40 40 LET O=T* (1. 5*W) 50 LET G=O 55 LET H=H-T 60 LET R=W*~: LET G=G+R 80 LET Y=Y+G 90 IF Y-G>=9200 THEN 150 100 GO TO 1310 ,110 LET F=(G-(Y-92CijCij»*5.2CijCij0CijE-02:GOTO 160 130 LET F=G*5.200CijCijE-Cij2:GO TO 160 150 LET, F=0 160 LET 1= (G-(D*13.5»*.14 1'70 LET N;::G- (I+F+V) . '180 PRINT "EMP NO",,"REG WAGE",,"O .T WAGE""",,"GROSS" r90 PRINT E" R" 0"" G: PRINT 200 PRINT"ITW"" "FI CA"" "VOL DEDUCT"" "YTD EARNINGS"" "NETPAY" 210 PRINT' I"F~V;Y"N -. '220 DATA 15722,,3,,410,,1,,501010,,12,,-999 230 GOTO 15 999 END READY . " : RUNNH COMPLETE PAYROLL DEMONSTRATION 'EMPLOYEE NUMBER 15722 HOURS WORKED= 410 HOURLY WAGE= 3 , EMP NO 15722 REG 'WAGE 120 o T WAGE . ·s lTV FICA 6.24 VOL· DEDUCT 12 14.91 ,READY 2-2 i GROSS 120 YTD EARNINGS 5120 NETPAY 86.85 When the program is executed (with the use of the RUN command), BASIC evaluates the statements in the order of their line numbers, starting with the smallest line number and going to the largest (regardless of the order in which they were typed or entered). 2.3 STATEMENTS Each line number is followed by an English word (key word) • The word identifies the type of statement and informs BASIC what to do or how to treat the data (if any) which follows the word. 2.3.1 Multiple Statements on a Single Line More than one statement can be written on a single line as long as each statement (except the last) is terminated with a colon. Thus only the first statement on a line can (and must) have a line number. For example: 19 PRINT "EDUCOMP BASIC" is a single statement line, while 2g LET X=l: PRINT X,Y,Z: IF X=2 THEN l~ is a multiple statement line containing three statements: a LET, a PRINT, and an IF-THEN statement. Any statement can be used anywhere in a multiple statement line except as noted in the discussion of the individual statements. 2.3.2 Continuation of a Single Statement onto Another Line single statement can be continued on the next line of the program. To type more than 72 characters (not including line numbers) on a line, simply continue typing after 72 characters. EDUBASIC will automatically perform a carriage return, line feed, tab, and allow you to ypte more characters. These two lines will be treated as one for GOTO's, IF-THEN, etc. The length of a multiple line statement is limited to 124 characters. A 2-3 2.4 CHARACTER SET User program statements are composed of individual characters. Allowable characters corne from the following character set: A through Z ~ through 9 and the following special symbols: Symbol $ Function Used in specifying string variables. Used to delimit string constants, i.e., text strings. Begins comment part of a line (section 3.9). Separates multiple statements on one line. Denotes a device or filename, or is used as an output format effector. Output format effector and list terminator Output format effector. Used to group arguments in an arithmetic expression. Arithmetic operators. n ·• · t , ·, () + - = < t * / t > spaces can be used freely throughout the program to make statements easier to read. For example : l' LET Cl instead of: = H* R l~LETCl=H*R Both of the above statements mean the same thing to BASIC and are stored exactly the same within the computer when the program is executed. If a program is too large, spaces may be removed to decrease the size because each space is a character and takes up space in the computer's memory. 2.5 EXPRESSIONS An expression is a group of symbols which can be evaluated by BASIC. Expressions are composed of numbers, variables, functions, or a combination of these, separated by arithmetic or relational operators. Expressions are created by the 2-4 programmer and inserted into the standard BASIC statements in order to perform the various operations which comprise the user program. The following are examples of expressions acceptable to EDUCOMP BASIC. 4 A7*(Bt2+l) X<Y Not all kinds of expressions can be used in all statements, as is explained in the sections describing the individual statements. In the following sections the reader is introduced to the elements which compose BASIC expressions. 2.5.1 Numbers Numbers"called numeric constants because they retain a constant value throughout a program, can be positive or negative and can contain up to six digits. Numeric constants are written using decimal notation, as follows: 2 -3.675 1234.56 -12345.6 -.000078 The following forms are not acceptable numbers in BASIC: 14 3 r.r However, BASIC can find the decimal expansion of those two mathematical formulas as shown below: lj is expressed as 14/3 ~is expressed as SQR(7) These formats are explained further in later sections. A number representation using the letter E allows further flexibility. If numbers were limited to six digits, a computer 2-5 would not be able to solve many problems involving large numbers. Consequently, rather than saying that BASIC can only accept numbers with a maximum of six digits, we say that BASIC has six digits of precision. Larger numbers can be written using the letter E to indicate "times ten to the power", thus: .999123456 can be written in BASIC as l23.456E-6 -1234569~. can be written in BASIC as -1.23456E7 This E format representation of numbers is very flexible in that the number .001 can be written as lE-3, OlE-I, lOOE-5, or any number of ways. If more than six digits are generated during any computation, the result of that computation is automatically printed in E format. (If the ex- ponent is negative, a minus sign is printed after the Ei if the exponent is positive, a plus sign is printed: lE-94i lE+94. The combination E7, however, is not a constant, but a variable. The term lE7 is used to indicate that 1 is multiplied by 10 7 • Numbers are specified according to the following rules: a. line numbers are unsigned decimal integers in the range 1 to 4094. b. 2.5.2 numbers have the range lE-6l6!n~E6l6. Variables A variable is a data item whose value can be changed by the programmer. A rLumeric variable is denoted by a single letter or by a letter followed by a single digit. Thus BASIC inter- prets E8 as a variable, along with A; X, N5, L9, and 01. (Subscripted and character string variables are described in later sections.) All variables are set equal to zero (9) 2-6 before program execution. Consequently it is only necessary to assign a value to a variable when an initial value other than zero is required, but it is good programming practice to initialize any variable. 2.S.3 Mathematical Operators BASIC automatically performs the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. Formulas to be evaluated are represented in a format similar to standard mathematical notation. There are five arithmetic operators used to write such formulae: I Example A+B A-B A*B AlB t A~B O~erator + * Meaning Add B to A Subtract B from A Multiply A by B Divide A by B Calculate A to the B power~AB When more than one operation is to be performed in a single formula, as is most often the case, rules are observed as to the precedence of the above operators. The arithmetic operations are performed in the following sequence, with (1) having the highest precedence: 1. Any formula inside parentheses is evaluated before the parenthesized quantity is used in further computations. Where parentheses are nested, as in (A+(B*(~2») 2. the innermost parenthetical quantity is calculated first. In the absence of parentheses in a formula, BASIC performs operations as follows: 1. exponentiation 2. unary minus 2-7 3. 4. multiplication and division addition and subtraction For example, -3t2=-(3)t2=-9 3. . In absence of parentheses in a formula involving more than one operation on the same level in (2) above, the operations are performed left to right, in the order that the formula is written. For example: AtBtC is evaluated as {AtB)tC A*B/C is evaluated as {A*B)/C The formula (or expression) A+B*CtD is evaluated as follows: first, C is raised to the D power second, the result of the first operation is multiplied by B third, the result of the previous operation is added to A. Parentheses are used to indicate any other order of evaluation. For example, if it is the product of Band C that is to be raised to the D power, the expression would look as follows: A+(B*C)tD If it is desired to multiply the quantity A+B by C to the D power: (A+B) *ct D The user is encouraged to use parentheses even where they are not strictly required in order to make the formulae easier to read. Ambiguities exist only in the programmer's mind, the computer always performs the operations as explained above. 2.5.4 Relational Symbols Relational symbols are used in IF-THEN statements (see section ~.4) where it is necessary to compare values. symbols are as follows: 2-8 The relational Mathematical BASIC Symbol Symbol Example = < = < A=B A<B A is equal to B < A<=B A is less than or equal to B > <= > A>B > >= A>=B A is greater than B A is greater than or equal to B t- <>or # A<>B, A#B A is not equal to B 2-9 Meaning A is less than B CHAPTER 3 ELEMENTARY BASIC STATEMENTS The simplest forms of the more elementary BASIC statements, are sufficient, by themselves, for the solution of most problems. Once these statements are mastered, the user can investigate the more advanced applications of these statements and the additional statements and features explained in later chapters. The reader should understand that any problem which can be solved with the more advanced techniques can also be solved with the simpler statements, although the solution may not be as efficient. As long as the user understands the details of his problem he can represent it in BASIC on a number of levels ranging from the simple to the sophisticated. 3.1 LET STATEMENT The LET statement assigns a numeric value to a variable. Each LET statement is of the form: line number[LET] variable = expression This statement does not indicate algebraic equality, but performs the calculations within the expression (if any) and assigns the numeric .value to the indicated variable. For example: 19 LET X=X+l 2g LET W2=(A4-Xt3) * (Z-A/B) In line 10, the old value of X is increased by one and becomes the new ~alue of X. In line 20, the formula on the right hand side is evaluated and the numeric value assigned to W2. The LET statement can be a simple numerical assignment, such as sg LET A=3S 3-1 or require the evaluation of a formula so long that it is continued on the next line (see Section 2.3.2). EDUCOMP BASIC allows the user to completely omit the word LET from the LET statement. The user may find it easier to type: l~ X=12* (S+7) l~ LET X=12*(S+7) than This convenience does not alter the effect of the statement. The LET statement can be used anywhere in a multiple statement line, for example: l~ X=44: Y=Xt2+Yl: B2=3.5*A The LET statement allows the user to assign a value to multiple variables in the same statement. l~ For example: LET X=Y=Z=5.7 causes each of the three variables to be set equal to 5.7. 3.2 PROGRAMMED INPUT AND OUTPUT This section describes the techniques used in performing BASIC program input and output (I/O). The most elementary forms of the PRINT, INPUT, READ, and DATA statements are described here so that the user is able to create simple BASIC programs. Using the LET statement; already described, and the following executable statements, the user can easily write a viable BASIC program of the simplest sort. If he should want to try his program, these simple I/O statements will provide a means of doing so and obtaining tangible output. These statements are described in detail at the end of this chapter and additional, more advanced, I/O techniques are described in later chapters. 3-2 The PRINT statement is used to output program results. The PRINT statement has the basic form: PRINT [list] line number -where the optional list can consist of messages to be printed or numeric values, or both. Without the list, the PRINT statement 111 PRINT causes a carriage return/line feed to be performed at the teleprinter. In order to print numeric values, the word PRINT is followed by the variable or expression whose numeric value is to be printed. The PRINT statement, like the LET statement, can perform numeric calculations. For example: 1$1 LET A=2: LET B=4 211 PRINT (A+B)*2 causes the number 12 to be printed when line 20 is executed. A message can be easily output on the teleprinter by enclosing the text to be printed in quotation marks, as follows: 7$1 PRINT .. STUDENT NUMBER = "; X This statement causes the following to be printed (where X=7744): STUDENT NUMBER =7744 The READ and DATA statements are used to input data to a .' program during execution. A DATA statement contains values which are assigned to the variables within a READ statement. When the execution of the program encounters a READ statement of the form: line number READ list the BASIC processor assigns to the first variable in the list the first available value encountered in the pool of DATA statements within the program. 3-3 The second variable is assigned the second value in the DATA pool, and so on. Var- iable names are separated by commas. A DATA statement looks as follows: line number DATA list DATA statements are usually grouped together toward the end of a program. All of the DATA statements in a given program are considered to be one data pool from which subsequent READ statements obtain values. (The values in the list are separated by commas.) The DATA statements are referenced in the order of their line numbers. l~ 2~ 3~ 4~ 5~ For example: READ A,B,C READ D,E,F READ A,B,C DATA 1~2,3,4 DATA 5,6,7,8,9 results in the following assignments being made: A=l B=2 C=3 when line l~ is executed D=4 E=5 F=6 when line 2~ is executed A=7 B=8 C=9 when line3~ is executed The INPUT statement allows the user to enter data to the program from the terminal keyboard while the program is being executed. The data is typed by the user as BASIC asks for it. For example: 111 INPUT A,B,C causes BASIC to pause during execution, print a question mark, and wait for the user to type three numerical values. The numbers must be separated by commas and terminated with the RETURN key. BASIC keeps printing question marks until 3-4 it obtains the desired number of numeric inputs from the keyboard. For example, line l¢ above would cause: ? to be printed. The user could type: ?15,24 followed by the RETURN key. BASIC would reply: ?15,24 ? and wait for the user to enter a third value. Any values entered beyond the number required (three in the above case) would be ignored. INPUT statements are used only when small amounts of data are to be entered, or when data can only be supplied while the program is running. 3.3 UNCONDITIONAL BRANCH, GOTO STATEMENT The GOTO statement is used when it is desired to unconditionally transfer to some line other than the next sequential line in the program. In other words, a GOTO statement causes an immediate jump to a specified line, out of the normal consecutive line number order of execution. The general format of the statement is as follows: line number GOTO line number The line number to which the program jumps can be either greater than, equal to, or less than the current line number. It is thus possible to jump forward or backward within a . program. Consider the following simple example: / '. 10 L:ET A=2 20 GOTO 50 30 LET A=SQRCA+14) 50 'PRI NT A~ A*A 60 END 3-5 When executed, the above lines cause the following to be printed: 4 2 When the program encounters line 20, control transfers to line 50; line 50 is executed, control then continues to the line following line 50. Line 30 is never executed. Any number of lines can be skipped in either direction. When written as part of a multiple statement line, GOTO should always be the last statement on the line, since any statement following the GOTO on the same line is never executed. ll~ 3.4 For example: LET A=ATN(R2): PRINT A: GOTO 5~ CONDITIONAL BRANCH, IF-THEN STATEMENT The IF-THEN is used to transfer conditionally from the normal consecutive order of statement numbers, depending upon the truth of some mathematical relation or relations. The basic format of the IF statement is as follows: line number IF condition The specified condition is tested. THEN statement THEN line number If the relationship is found false, then control is transferred to the line following the IF statement (the next sequentially numbered line). If the condition is true, the statement following THEN is executed or control is transferred to the line number given after THEN. The deciding condition is a simple relational expression in which two m~thematical expressions are separated by a relational operator. For example: Relational Expression A+2~B 3-6 The condition, when evaluated, is either true or false; no numeric value is associated with the result of an IF statement. The relational operators are described in Section 2.5.4 and are presented in Appendix A for reference. 75 IF A*B =B*(B+l) THEN LET 04=04+1 In the above line the quantities A*B and B*(B+l) are compared. If the first value is greater than or equal to the second value, the variable D4 is incremented by 1. If B*(B+l) is greater than A*B, 04 is not incremented and control passes tmmediately to the next line following line 75. When a line number follows the word THEN, execution is the same as if a GOTO statement followed the word THEN. The word THEN can be followed by any BASIC statement, including another IF statement. For example: 25 IF A'>B THEN IF B:::>C THEN PRINT ".A7B?C" The preceding line would perform the following operation: if B is both less than A and greater than C, the message A>B>C is printed, otherwise the line following line 25 is executed • . (The above example, line 25, is the same as "IF A>B AND B~C THEN PRINT "~B>C". This last form is not permitted in EDUCOMP BASIC. ) In the following example, the IF-THEN statement in line 20 is used to limit the value of the variable A in line 10. Execution of the loop continues until the relationship A>4 is true, then immediately branches to line 55 to end the program. (A program loop is a series of statements which are written so that, when the statements have been executed, control transfers to the beginning of the statements. This process continues to occur until some terminal condition is reached. ) 3-7 LISTNH 10 'LET A=A+ 1: X=At2 20 IFA>4 TH&~ 55 25 PRINT X 30 PRINT "VALUE OF A IS";A 40 GOTO 10 55 END READY When the above loop is executed, the following is printed: .RUNNH 1 VALUE OF A IS 1 4 VALUE OF A IS 2 9 VALUE 0 F A IS 3· 16 VALUE OF A IS 4 READY (The novice BASIC programmer is advised to follow the operation of the computer through these short example programs.) In IF statements, the following priorities are associated with each operator, in order to provide unambiguous· evaluation of the conditions specified (where a. has the highest priority): a.expressions in parentheses b. intrinsic functions c. exponentiation (t) d. unary minus (-), that is, a negative number or variable such as -3, -A, etc. e. multiplication and division (* and /) f. addition and subtraction (+ and -) g. relational operators (=,<,(=,),>=, # ,<» Within the operators indicated in anyone group above, operations proceed from left to right. 3-8 Examples of IF-THEN statements follow 1~ IF A)B THEN 1~¢ ! SIMPLE COMPARISON ASSIGNMENT BY A LET STATEMENT 2¢ IF A>B THEN A=-B An IF statement would normally be the last statement on a multiple statement line (to avoid confusion); however, the following rules govern the transfer path of the IF statement in other positions: a. The physically last THEN clause is considered to be followed by the next statement (or statements) on the line: l¢ IF A=l THEN PRINT Ai :PRINT "TRUE CASE": GOTO 2¢ 15 PRINT "NOT = 1" where A~l, the following line is printed: NOT = 1 where A=l, the following line is printed: 1 TRUE CASE b. All other THEN clauses are considered to be followed by the next line of the program: 2¢ IF A>B THEN· IF B>C THEN PRINT "B> C": GOTO 3¢ 25 PRINT "A<=B" Only in the case where "B>C" is printed is the statement GOTO 3¢ seen and executed. 3.4.1 LOGICAL IF-THEN It is sometimes useful to have available a somewhat different form of the IF-THEN statement. The following variation is called a logical IF-THEN; IF variable THEN statement If the value of the variable is zero, the statement is false and control is transferred to the next sequential line. If the value of the variable is anything other than zero, the statement is true and the specified exp!.ession is executed. For example, 3-9 -110 INPUT A 115 IF A THEN PRINT "A<>0": GOTO 110 120 PRINT "A=0" 200 END READY RUNNH ? 5 A<>0 ? -2 A<>0 ? .246 A<>0. ? 0 A=0 READY 3.5 PROGRAM LOOPS Loops were first mentioned in the section on the IF-THEN statement. Programs frequently involve performing certain operations a specific number of times. This is a task for which a computer is particularly well suited. with simple tasks, such as computing a list of prime numbers between 1 and 1,000,000, a computer can perform the operations and obtain correct results in a minimal amount of time. To write a loop, the programmer must ensure that the series of statements is repeated until a terminal condition is met. Programs containing loops can be illustrated by using two versions of a program to print a table of the positive integers 1 through 100 together with the square root of each. Without a loop, the first program is 101 lines long and reads: 3-10 ,. , UJ PRINT 1" SQR( 1) 20 PRINT 2" SQR(2) 30 PRINT 3" SQR(3) , -•- • • 990 PRINT 99" SQR(99) 1000 PRINT 100~ SQR(100) 101ft} END .- With the following program example, using a simple loop, the same table is obtained with fewer lines: . 10 LET X=1 20 PRINT X"SQRCX> 30 LET X=X+l 40 IF X<=1ft}0 THEN 2ft} 50 END Statement 10 assigns a value of 1 to X, thus setting up the initial conditions of the loop. square root are printed~ In line 20, both 1 and its In line 30, X is incremented by 1. Line 40 asks whether X is still less than or equal to 100; if so, BASIC returns to print the next value of X and its square root. This process is repeated until the loop has been executed 100 times. After the number 100 and its square root have been printed, X becomes 101. The condition in line 40 is now false so control does not return to line 20, but goes to line 50 which ends the program. All program loops have four characteristic par.ts: a. initialization, the conditions which must exist for the first execution of the loop (line 10 above); b. the body of the loop in which the operation which is to be repeated is performed (line 20 above); c. modification, which alters some value and makes each execution of the loop different from the one before and the one after (line 30 above); d. termination condition, an exit test which, when satisfied, completes the loop (line 40 above). Execution continues to the program statements following the loop (line 50 above). 3-11 3.5.1 FOR and NEXT Statements The FOR statement is of the form: line number FOR variable = expression TO expression [STEP expression For example: 19 FOR K=2 TO 2~ STEP 2 which causes program execution to cycle through the designated loop using K as 2, 4, 6, 8, • . . , 20 in calculations involving K. When K=20, the loop is left behind and the program control passes to the line following the associated NEXT statement. The variable in the FOR statement, K in the preceding example, is known as the control variable. The control variable must be unsubscripted, although a common use of such loops is to deal with subscripted variables using the control variable as the subscript of a previously defined variable (this is explained in further detail in Section 3.5.2). The expressions in the FOR statement can be any acceptable BASIC expression as defined in Section 2.5. The NEXT statement signals the end of the loop which began with the FOR statement. The NEXT statement is of the form: line number NEXT variable where the variable is the same variable specified in the FOR statement. Together the FOR and NEXT statements describe the boundaries of the program loop. If the STEP expression is omitted from the FOR statement, +1 is the assumed value. Since +1 is a common STEP value, that portion of the statement is frequently omitted. The expressions within the FOR statement are evaluated once upon initial entry to the loop. The test for completion of the loop is made prior to each execution of the loop. (If the test fails initially, the loop is never executed.) 3-12 The control variable can be modified within the loop. When control falls through the loop, the control variable retains the last value used within the loop. The following is a demonstration of a simple FOR-NEXT loop. The loop is executed 10 times; the value of I is 10 when control leaves the loop; and +1 is the assumed STEP value: 10 FOR I : 20 PRINT I 30 NEXT I 40 PRINT I 1 TO 10 The loop itself is lines 10 through 30. The numbers 1 through 10 are printed when the loop is executed. After 1=10, con- trol passes to line 40 which causes 10 to be printed again. If line 10. had been: l~ FOR I = l~ TO 1 STEP -1 the value printed by line 40 would be 1. 10 FOR I = 2 TO 44 STEP 2 20 LET I = 144 30 NEXT 1 The above loop is only executed once since the value of 1=44 has been reached and the termination condition is satisfied. If, however., the initial value of the variable is greater than the terminal value, the loop is not executed at all. statement of the format: l~ FOR I = 2~ TO 2 STEP 2 can not be used to begin a loop, although a statement like the following will initialize execution of a loop properly: l~ FOR 1=2~ TO 2 STEP -2 For positive STEP values, the loop is executed until the control variable is greater than its final value. For negative STEP values, the loop continues until the control variable is less than its final value. 3-:13 A FOR loops can be nested but not overlapped. The depth of nesting depends upon the amount of user storage space available (in other words, upon the size of the user program and the amount of core each user has available). Nesting is a programming technique in which one or more loops are completely within another loop. The field of one loop (the numbered lines from the FOR statement to the corresponding NEXT statement, inclusive) must not cross the field of another loop. ACCEPTABLE NESTING TECHNIQUES UNACCEPTABLE NESTING TECHNIQUES Two Level Nesting cS OR II = I TO Ira' FOR I2 = I TO Ira' [ NEXT I2 [ FOR I3 = I TO Ira' NEXT I3 NEXT II 0R II = I TO Ira' FOR I2 = I TO Ira' EXT II EXT I2 Three Level Nesting FOR II = I TO Ira' FOR I2 = I TO Ira' rFOR I3 = I TO 1~ LNEXT I3 r-FOR I4 = I TO l~ L..NEXT I4 EXT I2 NEXT II II = I FOR I2 = I ~OR I3 = I EXT I3 [!OR I4 = I EXT I4 NEXT II EXT I2 TO Ira' TO Ira' TO Ira' TO Ira' An example of nested FOR-NEXT loops is shown below: / DlM xes .. un 10 FOR A=1 TO 5 20 FOR B=2 TO 10 STEP 2 3S LET XCA.. i3>= A+B 1&0 NEXT B S0 NEXT A 5S PRINT xes .. 10> S .. -' ,. ~~ -'.- Upon execution of the above statements, BASIC prints 15 when line 55 is processed. 3-14 It is possible to exit from a FOR-NEXT loop without the control variable reaching the termination value. A conditional or unconditional transfer can be used to leave a loop. Control can only transfer into a loop which had been left earlier without being completed, ensuring that termination and STEP val~es are assigned. Both FOR and NEXT statements can appear anywhere in a multiplestaternent line. For example: lfl FOR I=l to If} STEP 5: NEXT I: PRINT "I="; I causes: I= 6 to be printed when executed. The FOR nor NEXT statement can be executed conditionally in an IF statement. The following statements are correct: 15 IF I<>J THEN NEX~ I 16 IF I=J THEN FOR I=l to J 3.5.2 Subscripted Variables and the DIM statement In addition to the simple variables which were described in Chapter 2, BASIC allows the use of subscripted variables. Subscripted variables provide the programmer with additional computing capabilities for dealing with lists, tables, matrices, or any set of related variables. In BASIC, variables are allowed one or two subscripts. The name of a subscripted variable is any acceptable BASIC variable name followed by one or two integer expressions in parentheses. For example, a list might be described as A(I) where I goes from 1 to 5 as shown below (all matrices are created with a zero element, even though that element is never specified) : 3-15 A(~), A(l), A(2), A(3), A(4), A(S) This notation allows the programmer to reference each of six elements in the list, which can be considered a one dimensional algebraic matrix as follows: A(~) A(l) A(2) A(3) A(4) A(S) A two dimensional matrix B(I,J) can be defined in a similar manner and graphically illustrated as follows: B (~ , 1) B(~,2) B(~,3) B(~,J) B(l,~) B(1,I) B(I,2) B(1,3) B(l,J) B(2,~) B(2,1) B(2,J) B(3,1) B (3, J) Subscripts used with subscripted variables throughout a program can be explicitly stated or be any legal expression. It is possible to use the same variable name as both a subscripted and an unsubscripted variable. Both A and A(I) are valid variables and .can be used in the same program. However, BASIC does not accept the same variable name as both a singly and a doubly subscripted variable name in the same program. If A(I) and A(I,J) are used in the same program, an error message 'ILLEGAL SUBSCRIPTING' results. A dimension (DIM) statement is used to define the maximum number of elements in a matrix. 3-16 {"Matrix" is the general term used in this manual to describe all elements of a subscripted variable.) The DIM statement is of the form: line number DIM variable (n), vari able (n ,m) , ... where the variables specified are indicated with their maximum subscript value(s). For example: 1~ DIM X(5), Y(4,2), 12 DIM 14(1.0.0) A(l.0,l~) Only integer values (such as 5 or 5070) can be used in DIM statements to define the size of a matrix. Any number of matrices can be defined in a single DIM statement as long as their representations are separated by commas. If a subscripted variable is used without appearing in a DIM statement, it is assumed to be dimensioned to length 10 in each dimension (that is, having eleven elements in each dimension, ~ through 1.0). 'However, all matrices should be correctly dimensioned in a program. DIM statements are usually grouped together among the first lines of a program. The first element of every matrix is automatically assumed to have a subscript of zero. Dimensioning A(6,10) sets up room for a matrix with 7 rows and 11 columns. This zero element is illustrated in the following program: LISTNH 10 REM - MATRIX CHECK PROGRAM 20 DIM A(6~leJ) 30 FOR 1=0 TO 6 40 LET A(I,,0) = I 50 FOR J=0 TO 10 60 LET A(0~J) = J 70 PRINT A( I" J) J 80 NEXT J: PRINT: NEXT I 90 END READY 3-17 " RUNNH e 1 e 2 3 1 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " 5 6 " 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 8 0 0 0 0 0 0 0 0 0 0 0 13 9 0 0 0 0 0 0 10 0 0 0 0 0 13 READY \ Notice that a variable has a value of zero until it is assigned a value. If the user wishes to conserve core space he may make use of the extra variables set up within the matrix. He could, for example, say DIM A(5,9) to obtain a 6 x 10 matrix which would then be referenced beginning with the A(~,~) element. The size and number of matrices which can be defined depend upon the amount of user storage space available. A DIM statement can be placed anywhere in a multiple statement line. A DIM statement can appear anywhere in the prog- ram and need not appear prior to the first reference to an array, although DIM statements are generally among the first statements of a program to allow them to be easily found if any alterations are later required. 3.6 MATHEMATICAL FUNCTIONS Within the course of a user's programming experience, he encounters many cases where relatively cornmon mathematical operations are performed. The results of these cornmon operations can often be found in volumes of mathematical tables; i.e., sine, cosine, square root, log, etc. Since it is this sort of operation that computers perform with speed and accuracy, such operations are built into BASIC. The user need never consult tables to obtain the value of the sine of 23° or the natural log of 144. When such values are to be used in an expression, intrinsic functions, such as: 3-18 SIN(23*PI/1BO) LOG(144) are substituted. The various mathematical functions available in. EDUCOMP BASIC are detailed in Table 3.1. Table 3.1 Mathematical Functions Function Code ABS (X) SGN (X) INT(X) COS (X) SIN (X) TAN (X) ATN(X) SQR(X) EXP(X) LOG (X) PI RND(X) Meaning returns the absolute value of X returns the sign function of X, a value of 1 preceded by the sign of X, SGN(¢)=¢ returns the greatest integer in X which is less than or equal to X, (INT(-.5)=-1) returns the cosine of X in radians returns the sine of X in radians returns the tangent of X in radians returns the arctangent (in radians) of X returns the square root of X returns the value of e~X, where e=2.71B2B ••• returns the natural logarithm of X, log X has a constant value of 3.41593. returns a random number between ¢ and li the same sequence of random numbers is generated each time a program is run requiring the use of the random number generator. The value of X is ignored. Most of these functions are self-explanatory. Those which are not are explained in the following section. 3.6.1 Examples of Particular Intrinsic Functions Sign Function, SGN(X) The sign function prints the value 1 if X is positive -1 if X is negative Oif X is zero. 3-19 Ff'r exa,Itlple:- i-. -- -LISTNH 10 REM - SGN FUNCTION EXAMPLE 20 READ-A6B 25 PRINT "A="lA6"B="1B 30 PRINT "SGNCA)="1SGNCA)6"SGNCB)="':SGNCB) 40 PRINT "SGNCINT(A»="':SGNCINTCA» 50 DATA -7.326 .44 -·'60 END READY RUNNH -A=-7.32 B= .44 SGN(A)=-l SGNeB)= 1 SGNCINT(A) )=-1 , - R_EADY -" . Integer Function, INT(X) The integer function returns the value of the greatest integer not greater than X. For example, INT(34.67) = 34. INT can be used to round numbers to the nearest integer by asking for INT(X+.5). For example, INT(34.67+.5) = 35. INT can also be used to round to any given decimal place, by asking for INT(X*l~t D+.5)/l~to where D is the number of decimal places desired, as in the following program: LISTNH 10 REM- INT FUNCTION EXAMPLE - 20 PRINT "NUMBER TO BE ROUNDED"': 30 INPUT A 40 PRINT "NO. OF DECIMAL PLACES" 1 50 INPUT D 60 LET B=INT(A*10-tD+.5)/10fD 70 PRINT "A ROUNDED- =-'"'1B 80 GOTO 20 90 END READY .I 3-20 .........-- ": RUNNH NUMBER TO BE ROUNDED? 55.6534 NO. OF DECIMAL PLACES? 2-A ROUNDED = 55.65 NUMBER TO BE ROUNDED? 78.375 NO. OF DECIMAL PLACES? -2 .A ROUNDED = 100 NUMBER TO BE ROUNDED? 67.89 NO. OF DECIMAL PLACES? -1 A, ROUNDED = 70 NUt·iBER TO BE ROUNDED? STOP AT LINE 30 READY For negative numbers, the largest integer contained in the number is a negative number with the same or a larger absolute value. For example: INT(-23) I but INT(-14.39) = -15. NOTE +P in the above program terminates program execution. .~:.. . Randon Number Function, RND(X) The random number function produces a random number between o and 1. The numbers are reproducible in the same order for later checking of a program. The argument X in the RND(X) function call can be any number, as that value is ignored. L·ISTNH .10 REli - RANDOM NtJr1BER EXAMPLE 25 PRINT '-RANDOM NUt/lEERS" 30 FOR 1=1 TO 30 40 PRINT RND(0)1 50 NEXT I 60 END READY ·RUNNH RANDOM NUt1BERS .770032.728066 ~395189 ~425557 ~281333 3.972J8E-02 .867253 READY ~751974 ~913388 ~566656 ~724634 ~-7306_64 .438103 ~076028 .51324 ~-955142 -'-963083 ~-182217 ~-650321 ~681433 -.-867935 . -'-990309 -'-107712 -.-420146 -'-896285 -'-235705 -'-834855 ~-57871 ,:'; ~-608095 ·.-169325 -",:'~ /"" 3-21 " "' In order to obtain random digits from 0 to 9 , change line 40 to read: 4~ PRINT INT(l.0*RND(O» , and tell BASIC to run the program again. This time the re- sults are: t RUNNH RAN DOH Ntn1BERS 7 3 4 ,2 " 8 7 7 9 S 7 7 II 5 1 " 9 9 6 1 6 8 9 5 2 8 6 1 II 8 READY t ~ •, •, It is possible to generate random numbers over any range. For example, if the range (A,B) is desired, use: (B-A)*RND(.0)+A to produce a random number in the range A<n<B. 3.6.2 ! RANDOMIZE Statement ~. The RANDOMIZE statement is written as follows: line number RANDOMIZE ,, } or, alternatively: line number RANDOM If the random number generator is to calculate different random numbers every time a program is run, the RANDOMIZE statement is used. RANDOMIZE is placed before the first use of random numbers (the RND function) in the program. When executed, RANDOMIZE causes the RND function to choose a random starting value, so that the same program run twice gives different results. For this reason, it is good practice to debug a program completely before inserting the RANDOMIZE statement. 3-22 To demonstrate the effect of the RANDOMIZE statement on two runs of the same program, we insert the RANDOMIZE statement as statement 15 in the following program: LISTNH 15 RANDOM I ZE' 20 FOR "1=1 TO 5 25 PRINT "VALUEII; 30 NEXT I 35 END I oil I IS"; RND(0) .-, READY RUNNH j VALUE 1 VALUE 2 VALUE 3 VALUE 4 VALUE 5 IS IS IS IS IS .808118 -.1342323 ".-780877 -'-104348 -'-598201 READY ~UNNH VALUE. 1 IS .572767 VALUE 2 IS -'-136269 VALUE 3 . IS -'-662712 VALUE 4 IS -.-749856 VALUE 5 IS -.-534725 READY " - - . --.,~ The output from each run is different. 3.7 SUBROUTINES A subroutine is a section of code performing some operation required at more than one point in the program. Sometimes a complicated I/O operation for a volume of data, a mathematical evaluation which is too complex for a user-defined function, or any number of other processes may be best performed in a subroutine. More than one subroutine can be used in a single program, in which case they can be placed one after another at the end of the program (in line number sequence). A useful 3-23 practice is to assign distinctive line numbers to subroutines; for example, if the main program uses line numbers up to 199, use 200 and 300 as the first numbers of two subroutines. LISTNH 1 REM - THIS PROGRAM ILLUSTRATES GOSUB AND RETURN 2", INPUT ~ B" C 3'" GOSUB 100" 140 LET A=ABSCINTCA» 50 -_ LET B=ABS CINTCB» 60 LET C=ABSCINTCC» 70 PRINT 80 GOSUB 100 9'" STOP 1"'0 REM - THIS SUBROUTINE PRINTS OUT THE SOLUTIONS 11'" REM - OF THE EQUATION: AXf2 + BX + C = 0 12'" PRINT uTHE EQUATION IS It; A ;n*Xf2 + n; B ;"*X + n; C 13'" LET D=B*B - 4*A*C 14'" IF 0<>0 THEN 170 15'" PRINT "ONLY ONE SOLUTION ... X "; -B/C2*A) 16'" RETURN ~ 17'" IF 0<0 THEN 200 180 PRINT "TWO SOLUTIONS ••• X =n; 185 PRINT C-B+SQRCD»/C2*A); "AND X ="; C-B-SQRCD»/C2*A) 19'" RETURN PRINT "IMAGINARY SOLUTIONS ••• X = Cn ; 2"'5 PRINT -B/ C2*A) ; It" It;_ SQRC -DY/C 2*A) ; It) AND Cit J 2"'7- PRINT -B/C2*A) ;";''';-SQR(-D)/C2*A) ;ft)" 21'" RETURN .9"'0 END 2"' ' . READY RUNNH ? 1#.5" -.5 THE" EQUATION IS 1 *Xf2 + .5 *X + -.5 TWO SOLUTIONS ••• X = .5 AND X =-1 THE EQUATION IS 1 *Xf2 + 0 *X + 1 IMAGINARY SOLUTIONS ••• X = C '" " 1 ) AND C 0 .. -1 ) STOP AT LINE 90 READY Lines 100 through 210 constitute the subroutine. The subroutine is executed from line 30 and again from line 80. When control returns to line 90 the program encounters the STOP statement and terminates execution. 3-24 3.7.1 GOSUB Statement Subroutines are usually placed physically at the end of a program before DATA statements, if any, and always before the END statement. The program begins execution and cont- inues until it encounters a GOSUB statement of the form: line number GOSUB line number where the line number following the word GOSUB is the first . line number of the subroutine. that line in the subroutine. 5~ GOSUB Control then transfers to For example: 2~~ Control is transferred to line 2~~ in the user program. The first line in the subroutine can be a remark or any executable statement. 3.7.2 RETURN Statement Having reached the line containing a GOSUB statement, control transfers to the line indicated after GOSUB; the subroutine is processed until the computer encounters a RETURN statement of the form: line number RETURN which causes control to return to the statement following the original GOSUB statement. via a RETURN statement. A subroutine is always exited Before transferring to the subroutine, BASIC internally records the next sequential statement to be processed after the GOSUB statement; the RETURN statement is a signal to transfer control to this statement. In this way, no matter how many subroutines or how many times they are called, BASIC always knows where to go next. 3.7.3 Nesting Subroutines Subroutines can be nested: that is, one subroutine can call another subroutine. If the execution of a subroutine encounters 3-25 a RETURN statement, it returns control to the line following the GOSUB which called that subroutine. Therefore, a subroutine can call another subroutine, even itself. Subroutines can be entered at any point and can have more than one RETURN statement. It is possible to transfer to the beginning or any part of a subroutine; multiple entry points and RETURNs make a subroutine more versatile. The maximum level of GOSUB nesting is dependent on the size of the user program and the amount of core storage available at the installation. 3.8 STOP AND END STATEMENTS The STOP and END statements are used to terminate program execution. The END statement is the last statement in a BASIC program. The STOP statement can occur several times throughout a single program with conditional jumps determining the actual end of the program. The END statement is of the form: line number END The line number of the END statement should be the largest line number in the program, since running a program with line numbers greater than that of the END statement results in the following error message being printed: ·1 END I NOT LAST and execution is halted. NOTE A program will execute without an END statement; however, the following error message is printed: NO 'END' STATEMENT. The STOP statement is of the form: line number STOP 3-26 and causes: STOP AT LINE READY line number to be printed when executed. Execution of a STOP or END statement causes the message: READY to be printed by the teleprinter. This message signals that the execution of a program has been terminated or completed, and BASIC is able to accept further input. 3.9 REMARKS AND COMMENTS It is often desirable to insert notes and messages within a.user program. Such data as the name and purpose of the program, how to'use it, how certain parts of the program work, and expected results at various points are useful things to have present in the program for ready reference by anyone using the program. There are two ways of inserting comments into a user program: a. b. the REMARK statement, and use of the exclamation mark (1). The REMARK statement must be preceded by a line number. The word REMARK can be abbreviated to REM for typing convenience, and the message itself can contain any printing character on the keyboard. BASIC completely ignores anything on a line following the letters REM. (The line number of a REM statement can be used in a GOTO or GOSUB statement; see sections 3.4 and 3.8.1, as the destination of a jump in the program execution.) Typical REM statements are shown below: l~ REM - THIS PROGRAM COMPUTES THE AMOUNTS 11 REM - AND WRITES THE CHECKS 3-27 The exclamation mark is used to terminate the statement part of a line and begin the comment part of the line. For example: 125 LET Pl=(H-4.0')*R 130 PRINT P + PI !SET EQUAL TO OVERTIME PAY !PRINT SUM OF OVERTIME AND REGULAR PAY BASIC ignores everything on the line after encountering the exclamation mark. Messages in REMARK statements are generally called remarks, those after the exclamation mark, comments. Remarks and comments are printed when the user program is listed but do not affect program execution. It is good programming practice to include REMARKs and comments in all programs, unless space requirements are critical. 3.10 ON-GOTO STATEMENT The simple GOTO statement allows the user to unconditionally transfer control of the program to another line number. The ON-GaTO statement allows control to be transferred to one of several lines depending on the value of an expression at the time the statement is executed. The statement is of the form: line number ON expression GOTO list of line numbers The expression is evaluated and the integer part of the expression is used as an index to one of the line numbers in the list. For example 5.0' ON X GaTO 1.0'.0',2.0'.0',3.0'.0' transfers control to line number 1.0'.0' if the value of X is 1, to line number 2.0'.0'if X is 2, and to 3.0'.0' if X is 3. Any other values of X (other than 1,2, or 3 in this example) would cause a transfer to the next line. 3.11 ON-GaSUB STATEMENT The GOSUB and RETURN statements are used to allow the user to 3-28 transfer control of his program to a subroutine and return from that subroutine to the normal course of program execution (see Section 3.7 for details). The ON-GOSUB state- ment is used to conditionally transfer control to one of several subroutines or to one of several entry points to one (or more) subroutine(s) . The statement is of the form: line number ON expression GOSUB list of line numbers Depending on the integer value (truncated if necessary) of the expression, control is transferred to the subroutine which begins at one of the line numbers listed. Encounter- ing the RETURN statement after control is transferred in this way allows the program to resume execution at the line following the ON-GOSUB line. An example of the statement follows: 8~ ON X-Y GOSUB 9~~,933,1~14 When line 80 is executed, the value of X-Y being either 1, 2, or 3 causes control to transfer to line 900, 933 or 1014, respectively. If the quantity X-Y is not equal to 1, 2 or 3, control is transferred to the next line. Since it is possible to transfer into a subroutine at different points, the ON-GOSUB statement could be used to determine which portion of the subroutine should be executed. 3-29 CHAPTER 4 'CHARACTER STRINGS 4.1 CHARACTER STRINGS The previous chapters describe the manipulation of numerical information; however, EDUCOMP BASIC also processes information 'in the form of character strings. A string, in this context, is a sequence of characters treated, as a unit. A string can be composed of any combination of the ASCII characters in Table 4-2. Without realizing it, the reader has already encountered character strings. Consider the following program which prints the name of a month, given its number: LISTNH 10 PRINT "TYPE A NUMBER BETWEEN 1 AND 12"; 12 INPUT N 15·IF N>l THEN IF N<12 THEN IF N=INTCN) THEN 20 17 PRINT "NUMBER OUT OF RANGE": GO TO 10 20 IF N>3 THEN PRINT "THE";NJ nTH MONTH IS"; 25 IF N= 1 THEN PRIN T "THE FIRST MON TH I S JANUARY" '30 IF N=2 THEN PRINT "THE SECOND MONTH IS FEBRUARY" 35 IF N=3 THEN PRINT "THE THIRD MONTH IS MARCH" , 40 IF N=4 THEN PRINT "APRIL" 45 IF N=5 THEN PRINT ''rIAY'' "I 50 IF N=6 THEN PRINT "JUNE" 55 IF N=7 THEN PRINT "JULY" . 60 IF N=8' THEN PRINT "AUGUST" 65 IF N=9 THEN PRINT "SEPTEr-1BER" '. 70 IF N=10 THEN PRINT ·'OCTOBER'" 75 IF ~= 11 THEN PRINT ·'NOVEMBER" 80 IF N= 12 THEN PRINT "DECEl1BER',' as END , RUNNH TYPE A NUMBER BETWEEN 1 AND 121 2 THE SECOND MONTH I S FEBRUARY READY '" I "., J 4-1 In Chapter 3 the INPUT and PRINT statements were shown printing messages along with the input and output of numeric values (see lines 10 and 15 above). These messages consist of char- acter string constants (just as 4 is a numeric constant). In a similar way, there are character string variables and functions. 4.1.1 String Constants Just as numbers can be used as constants or referenced by variable names, EDUCOMP BASIC permits character string constants. Character string constants are delimited by double quotes. For example: 1.5 LET Y$ = "FILE4" 8' IF A$ = "YES" THEN 25~ where "FILE4" and "YES" are character string constants. 4.1.2 Character String Variables Variable names can be introduced for simple strings and for lists composed of strings (which is to say one dimensional string matrices). Any single letter followed by a dollar sign($) character is a legal name for a string variable. For example: A$, C$, Z$ are simple string variables. Any single letter list var- iable name followed by the $ character denotes the string form of that variable. For example: V$(N), C$(M) are list string variables, (where M and N indicate the position of that element of the matrix within the whole) • The same name can be used as a numeric variable, as a string variable and as a one dimensional array in the same program. For example: A A$ A(N) 4-2 can all be used in the same program, but A(N) and A(M,N) cannot both occur in the same program. Just as numeric variables are automatically initialized to f1 when a program is run, string variables are initialized to a null string containing zero characters (the character . string constant ""). 4.1.3 Subscripted String Variables String lists are defined with the DIM statement, as are numerical lists and matrices. For example: 1tl DIM S$(5) indicates the S$ is a string matrix with six elements, S${~) through S$(5), which can be separately accessed. If a DIM statement is not used, a subscripted string variable is assumed to have a dimension of 10 (11 elements including the zero element) in each direction. Note that the dimension of a string array specifies the number of strings and not the number of characters in anyone string. For example, if 10 FO R 1= 1 TO 7 20 LET B$C I )="PDP-S" 30 NEXT I they would cause a list B$(n) to be created having 11 accessible elements, B$(~) through B$(l~). The elements B$(l) through B$(7) are set equal to "PDP-8" and the others would be null strings (have no characters). As a general rule, all lists should be dimensioned to the maximum size being referenced in the program. 4-3 4.1.4 String Size A character string can contain almost any number limited usually by the amount of memory storage available. In EDUCOMP BASIC the upper limit on string size is 2050 characters. The DIM statement is used not only to define an array, but also to indicate the length (number of characters) of a string. In EDUBASIC, strings longer than fifteen (15) characters must be dimensioned before they are accessed. For example: l~{l A$ = "f,J123456789.0'123456789" 2~.0' END The above example will generate an error message when executed, STRING OVERFLOW IN LINE 1.0'f,J. In the above example, line 9f,J should be added, 9' DIM A$ = 2.0' Strings must be dimensioned for the maximum length which they will assume in the user's program. However, a string may contain fewer characters than the number specified in the DIM statement. For example, l~' DIM A $ = 5f,J ll~ A $ = "EDUCOMP" l2~ . END The length of A$ will be seven after this program is executed. If no length is specified for stri~g variables, a length of fifteen is assumed. The. following line is an example of DIMensioning for string arrays: Iflfl DIM A.$ = 3.0'{l, B $(1f,J) , C.$(12) = 24 The above statement would reserve space in memory for 1. A character string of length 3.0'.0', 2. Eleven strings of length fifteen, and 3. Thirteen strings with twenty-four characters. 4-4 4.1.5 Relational Operators When applied to string operands, the relational operators indicate alphabetic sequence. 55 For example: IF A$(I) < A$(I+1) GOTO l~~ When line 55 is executed the following occurs: A$(I) and A$(I+1) are compared; if A$(I) occurs earlier in alphabetical order than A$(I+l), execution continues at line 100. Table 4-1 contains a list of the relational operators and their string interpretations. Table 4-1 Relational Operators Used With String Variables , Operator Example Meaning = < A$ = B$ A$ < B$ The strings A$ and B$ are equivalent. The string A$ occurs before B$ in alphabetical sequence. <= A$ <= B$ The string A$ is equivalent to or occurs before B$ in alphabetical sequence. > A$ > B$ The string A$ occurs after B$ in alphabetical sequence. >= A$ >= B$ The string A$ is equivalent to or occurs after B$ in alphabetical sequence. <>,# A$ # B$ The strings A$ and B$ are not equivalent. In any string comparison, trailing blanks are part of the string. That is to say "YES" is not equivalent to "YES II. A null string (of length zero) is considered to be completely blank and is less than any string of length greater than zero. 4-5 Table 4-2 ASCII Character Codes ASCII Decimal CharValue acter 91 1 2 3 4 5 6 7 8 9 191 11 12 13 14 15 16 17 18 19 291 21 22 23 24 25 26 27 28 29 391 31 32 33 34 35 36 37 38 39 491 41 42 - RSTS Usage NUL FILL character SOH STX ETX CTRL/C EOT ENQ ACK BEL BELL BS HORIZONTAL TAB HT LINE FEED LF VT VERTICAL TAB FF FORM FEED CR CARRIAGE RETURN SO SI CTRL/O DLE DC1 DC2 DC3 DC4 NAK CTRL/U SYN ETB CAN EM SUB ESC FS GS RS US SP ! CTRL/Z ESCAPE l ASCII Decimal CharValue acter 43 44 45 46 47 48 49 591 51 52 53 54 55 56 57 58 59 691 61 62 ·63 64 65 66 67 68 69 791 71 SPACE 72 73 74 75 76 II 77 # $ 78 79 891 81 82 83 84 85 % & I ( ) * RSTS Usage ASCII Decimal CharValue acter E F G 86 87 88 89 991 91 92 93 94 95 96 97 98 99 19191 1911 1912 1913 1914 1915 1916 1917 1918 1919 1191 III 112 113 114 H l1S I 116 117 118 119 1291 121 122 123 124 125 126 127 + , - . / 91 1 2 3 4 5 6 7 8 9 : J < = > ? @ A B C D J K L M N 0 P Q R S T U RSTS Usage V W X y Z [ \ ] " or + _ or 04.. Grave accent a b c d e f g h i I j k 1 m n 0 P q r s t u v w x Y z I { Vertical Line - Tilde DEL RUBOUT lALTMODE (ASCII 125) or PREFIX (ASCII 126) keys which appea~ on some terminals are translated internally into ESCAPE. NOTE The decimal values 128 through 255 can appear in character strings. For most practical purposes, the characters repre4-6 sented by N and N+128 (decimal) are the same. The characters CHR$(N) and CHR$(N+128) test as equal if compared. Users should be careful when performing output of these values since they may have some significance in certain devicedependent operations. 4.2 STRING INPUT The READ, DATA and INPUT statements can be used to input string variables to a program. For example, 19 READ A$, B, C, D 29 DATA 17, 14, 13.4, CAT causes the following assignments to be made: A$ = the character string "17" B = 14 C = 13.4 reading D as CAT causes the message BAD INPUT IN LINE 19 to be printed. EDUBASIC then tries to read the next number for D. In this example, no number exists after CAT so another error message is printed OUT OF DATA IN LINE 19. Quotation marks are necessary around string items in DATA statements only if the string contains a comma or if leading blanks within the string are significant. Quotes are always acceptable around string items, even though not always necessary. For example, the items in line 40 in the following program are all acceptable character strings and would be read as printed. EDUBASIC will recognize imbedded and trailing blanks even though there are no quote marks around the string. The comma, carriage return, or second qUvte is the end of the string. 4-7 .. / 10 READ AS .. BS" C$ .. OS .. E$ 29 PRINT A$;B$;C$;D$;E$ 39 PRINT A$ .. B$.. C$ .. D$ .. E$ 49 DATA "MR;' JONES .... MISS SMITH.. 50 END "HRS. BROlJt·,p'" I READY RUNNH MR. JONESMISS SMITHMRS. BBOWNMISSMR MR~ JONES . MISS SMITH ·MRS. BROWN MR MISS A READ statement can appear anywhere in a multiple statement line, but a DATA statement must be the last statement on a line. NOTE The data pool composed of values from the programmed DATA statements is stored internally as an ASCII string list. Where a numeric variable is read, the appropriate ASCII to numeric conversions are performed. Where a string variable is read, the string is used as it appears in the DATA statement. If the item did not appear in quotes, leading spaces are ignored. If the item did appear in qUotes, the string variable is equated to the entire string within the quotes. A feature of the INPUT statement when used with character string input is the INPUT LINE statement of the form: line number INPUT LINE string variable For example, l~ INPUT LINE A$ causes the program to accept a line of input from the terminal with punctuation characters or quotes. Any characters are acceptable in a line being input to the program in this manner. The program can then treat the line as a whole or in smaller segments as explained in Section 4.4 which describes string functions. An INPUT LINE statement reads the entire line as typed by the user, excluding the line terminating character. 4-8 The line terminator is a carriage return/line feed, generated by typing the RETURN key. 4.3 STRING OUTPUT When character string constants are included in PRINT statements, only those characters within quotes are printed. leading or trailing spaces are added. No For example, LlSTNH 10 X= 1. 0:Y=2. 01: A$="A=" 29 PRINT A$.nO "B="';Y 30 PRINT "DONE" 40 END READY RUNMH A= 1 B= 2.01 OOME READY Character string output can also contain the string functions described in the next section. 4.4 STRING FUNCTIONS Like the intrinsic mathematical functions (e.g., SIN, LOG), EDUCOMP BASIC contains various functions for use with character strings. These functions allow the program to concatenate two strings, access part of a string, determine the number of characters in a string, and perform other usefull operations. (These functions are particularly useful when dealing with whole lines of alphanumeric information input by an INPUT LINE statement). The various functions available are summarized in Table 4-3. 4-9 Table 4-3 String Functions! Function Code Meaning MID(A$,Nl,N2) Indicates a substring of the string A$ starting with character Nl, and N2 characters long (the characters between and including the Nl through N1+N2-1 characters of the string A$). For example: 100 PRINT MID(A$,lS,S) 110 END RUNNH OPQRS LEN (A$) Indicates the number of characters in the string A$ (including trailing blanks) • For example: 100 PRINT LEN(A$) 110 END RUNNH 26 + Indicates a concatenation operation on two strings. For example "ABC"+IIDEF" is . equivalent to IIABCDEF". 1112"+"3411+"56" is equivalent to "123456". CHR$(N) Generates a one-character string having the ASCII value of N (see Table 4-2). For example: CHR$(65) is equivalent to IIAII. Only one character can be generated. ASCII (A$) Generates the ASCII value of the first character in A$. For example, ASCII (IIX") is equivalent to 88, the ASCII equivalent of X. If B$ = IlXABII, then ASCII (B$) = 88. lA$ in the 'MID' and 'LEN·' examples is assumed to be "ABCDEFGHIJKLL\1NOPQRSTUVWXYZ". 4-10 CHAPTER 5 DATA STORAGE CAPABILITIES 5.1 FILE STORAGE Thus far, techniques have been presented for entering data into a program as it is written (via READ and DATA statements) or when it is executed (via the INPUT statement). Both of these techniques pose operational problems when the amount of data a program reads or writes is increased beyond a few items. 'In order to alleviate these problems, EDUBASIC provides the user with a facility to define Input/Output files. An EDUBASIC file consists of a sequence of 'data which is transmitted to (or from) a BASIC program from (or to) an external Input/Output device. The external device can be the user's terminal, the OS/S system disk, a line printer, magnetic tape, or high-speed paper tape equipment. Each file has both an • external name by which it is known within the system and an • ~ternal file designator (a number used to refer to the file Within the program). An OPEN statement is used to associate an external name with an internal designator. - --- An external file name is completely specified with the following information: device:filename.extension where the device can be one of the following: SYS: DSK: DTA~ to DTA7: PTR: PTP: LPT: CDR: TTY: RKA~ RKAI RKA2n-2: RKA2n-l: system device default device DECtape units 0 to 7 high-speed paper tape reader high-speed paper tape punch line printer card reader user's terminal system halt of an RKSe disk other half of an RKSe disk RKSe uni ts for n=2, 3 , 4' 5-1 The filename is a six character (maximum) alphanumeric name. The extension is a two character (maximum) alphanumeric file name extension usually specifying the type of file. used by the system are as follows The extensions (the user can create his own extensions): .BA BASIC source program, ASCII format .BC Compiled BASIC program, 'binary' format .DA Data file (sequential) .BR BASIC Random access data file (virtual file) A user can have up to 4 files open (with internal designators I through 4) for access at any given time. consumes a buffer within core storage. Each open file The buffer sizes for various devices are all 256 words under OS/8. - If a buffer cannot be created for a file, due to a lack of storage space in core, then the file cannot be opened. (The process of opening a file is described in section 5.2). 5.2 OPEN STATEMENT The OPEN statement is used to associate a file on a bulk storage device or an I/O device with an internal file designator. ~ This statement allows the file to be readily referenced in INPUT, PRINT, and (in some cases) DIM statements. The format of the OPEN statement is as follows: line number J INPUT OUTPUT OPEN /! V K~ _ -:-4- > ~ l / AS FILE expression r, The string field is a character string constant, variable orj;Yexpression that contains the €xterna-r-file specificati6i:1)--of .,,-,-.--....... the file to be opened. The AS FILE expression must have an "~'.~~" '-'.-.,,~,---,,-.,,~,".----,,~~-,~-......--,-.- '"." ,-~~-~. -~, - ' " ' -,- ,,-.,. -,.~.""""-) ~teger value between I and 4, corresponding to the internal channel number on which the field is being opened. There are three distinct forms for the OPEN command: 5-2 OPEN<string> FOR INPUT OPEN<string> FOR OUTPUT OPEN<string> The form of the OPEN statement used determines whether an existing file is to be opened or a new file created. a. b. c. An OPEN FOR INPUT statement causes a search for an already existing file (since the statement indicates the file is an input file). If no file is found, the FILE NOT FOUND error occurs. In the following examples the extensions .DA are assumed unless the extensions are provided. 5~ OPEN "FILE" FOR INPUT AS FILE I An OPEN FOR OUTPUT statement causes a search for an already existing file which, if found, is deleted. A new file is then created. 75 OPEN "DATA" FOR OUTPUT AS FILE 3 An OPEN statement without an INPUT or OUTPUT designation attempts to perform an OPEN FOR INPUT operation as described above. If this fails, a new file is created. l~~ OPEN "MATR.BR" AS FILE 4 The extension .BR is assumed if not specified. EDUBASIC permits access to data files by two methods: a. b. 5.2.1 Formatted ASCII and Virtual core arrays. Formatted ASCII I/O Formatted ASCII data files are the simplest method of data storage, involving a logical extension of the PRINT and INPUT statements to be used in conjunction with the OPEN statement. 5-3 The formats for INPUT and PRINT statements to be used with the OPEN statement are as follows: line number INPUT # expression , list line number PRINT # expression , list· where the expression has the same value as the expression in the OPEN statement (the internal file designator) and the list is a list of variable names, expressions, or constants as explained in the Sections describing the PRINT and INPUT statements. (The virtual array dimension statements reference OPEN statements without the FOR INPUT or FOR OUTPUT phrase, as explained later.) For example, 116 OPEN "CDR:" FOR INPUT AS FILE Nl 216 INPUT #Nl, A$ Line number 116 above causes the card reader to be opened as an input source with the internal file designator whose value is contained in the variable Nl. Line number 216 causes input to be accepted from logical I/O channel Nl; and the input is associated with the variable A$. (Nl must have a value between 1 and 4.) 5.2.2 File-Structured Vs. Non-File-Structured Devices OS/S distinguishes between file-structured (disk, DECtape and magtape) devices and non-file-structured (all other) devices. When a file is to be found or created on a file-structured device, the file specification string in the OPEN statement must include both a device designation and a filename. On non-file-structured devices, the device name alone identifies a file (filename and extension, if specified, are ignored.) For example: DTAl: is insufficient information to specify a file 5-4 DTAl:FRED is sufficient to specify the file FRED on DEC tape unit 1 PTP: uniquely specifies the high-speed punch PTP:FILE produces the error message FOUND IN LINE xxx FILE NOT File specification syntax is such that the default device need not be specified. For example: DSK:QUIZ is equivalent to: QUIZ When a device is not specified, a file name alone always indicates a disk or DECtape as a default storage device. To store a file on DECtape (other than the default device) the device would be specifically indicated: DTA4:FOO The following sequence is useful and allows for easy change in the device to be used before program execution begins: 111 LET I$ = "PTR:" 211 OPEN I$ FOR INPUT AS FILE 1 311 INPUT #1, A$ If a file being opened for input does not exist, an error message is returned. If a file being opened for output does not exist, it is created. If a file for output already exists it is deleted and recreated. If an assignable device is referenced in any OPEN statement and that device is unavailable for assignment, an error message is printed. 5-5 File names used in an OPEN statement are composed of up to six alphanumeric characters with an extension of up to two alphanumerics. Thus, an output file could be created as follows: 19 OPEN "DSK:SCRTCH.TM" FOR OUTPUT AS FILE N1 Thereafter, reference can be made to file SCRTCH.TM on device DSK: as follows (notice that the internal file designator is represented as a variable, although its value must still be between 1 and 4): 199 PRINT #Nl, A$, B$ 5.2.3 Opening the User Terminal as an I/O Channel The internal file designator (following the # character in the INPUT or PRINT statements) is always in the range I to 4. File designator g is, by defiz:1,ition, always open as the user's ter, minal. Internal file designator ~ cannot be closed or opened. , Use of file #g is indicated below (no OPEN #~ statement is necessary or allowed). 1~ INPUT #~, A$ is equivalent to: l ' INPUT A$ It is sometimes useful to be able to request keyboard input without having the "?" prompting character printed first. This can be accomplished by opening the user's terminal ("TTY:") on some internal file designator other than~. The? character is only generated for input requests on file #~,' as shown in the following example: / ,I 5 DIM A$=50 10 OPEN "TTY~·' FO RINPUT AS FILE 1 20 PRINT "WI TH USE OF INTERNAL_ FILE DESIGNATOR" 39 PRINT "TYPE YOUR NAME" FOLLOWED BY A RETURN KEY" AND A CTRL/Z" 40 INPUT ill" A$ 59 PRINT: PRINT 610 PRINT "FOR COMPARISON,,' VITHOUT FILE DESIGNATOR" 79 PRINT "TYPE YOUR NAME FOLLOWED BY A RETURN KEY" ,89 INPUT A$ 99 END .l " \ .• " 5-6 ., RUNNH WITH USE OF INTERNAL FILE DESIGNATOR TYPE YOUR NAME" FOLLOlvED BY A RETURN KEY" J. P. clONES . AND A CTRL/Z \ FOR COMPARISON" WITHOUT FILE DESIGNATOR TYPE YOUR NAME FOLLo\.JED BY A RETURN KEY ? cl. P. JONES READY . \ If a file is being opened for both input and output or to be referenced as virtual arrays the form: . Line number OPEN string AS FILE expression .is used. If the file indicated by the name "string" is found, it will be used and, if it is not found, it will be created. When a program used a statement such as: 511 OPEN "Faa" AS FILE 4 it can perform input and output to that file. However, such a file (Faa on the system device) can only be referenced in a sequential fashion. If data is already in the file, it can be read via INPUT statements similar to the manner in which a READ statement pulls data from the DATA statement pool. Any attempt to use a PRINT statement with the file Faa will work only if there is nothing already in that file. If data already exists in the file Faa, a PRINT statement will begin to write over any data beyond the point where the INPUT stopped. Thisis not a recommended technique since the entire file may be garbled and useless. 5.3 OUTPUT TO VARIOUS DEVICES In order to direct output to a device other than the user terminal, the PRINT command is formatted as follows: 5-7 line number PRINT # expression , list where the expression is the internal file designator of a previously opened output file (see section 7.2). The list of information to be output can include any of the output information described as applicable to the PRINT statement. For example: 1~ OPEN "DATAl" FOR OUTPUT AS FILE 1 2~ PRINT #1, "START OF DATA FILE" The above lines open a file called DATAl on the system device with internal file designator #1 (of 4 possible open files available in the system). The first line in that file reads: START OF DATA FILE. To output a table of square roots on the line printer, the following program could be used: 110 LET I $~ltLPT: 20 OPEN I$- FOR OUTPUT AS FILE 1 310 FOR I 1 TO 5: PRINT #l~ II SQRCI): NEXT I It 4fO-END = READY The results would appear on the line printer as follows: { 2 3 4 5 1 1.41421 _ 1'-732105 22.23607 It is. advisable to print 'only one character string per PRINT statement because terminators are not automatically introduced. 5-8 The carriage return serves as the delimiter. A MID function may be used to separate the fields as desired. 5.4 INPUT FROM VARIOUS DEVICES Like the PRINT statement, the INPUT statement can operate upon devices other than the user terminal. The form: line number INPUT # expression , list causes input to be accepted from the previously opened file or "device indicated in the expression (see section 5.1). As long as the value of the expression is non-zero, the specified file is read through one of the available user I/O buffers (internal file designators). If the expression is zero, or missing completely, input is from the user terminal. No? character is printed on the terminal paper when input is requested from a device other than the terminal, opened on file #~. For example: l~ OPEN "PTR:" FOR INPUT AS FILE 3 2~ INPUT #3, A$ causes the string A$ to be read from the high-speed paper tape reader. Note that spaces are ignored in numeric input data. Commas are inserted automatically when printing out to a data file. When inputting from a data file, a comma or carriage return is taken as a terminator. Once a file is opened it can be closed (a CLOSE statement must be used) with a second OPEN statement. Closing and reopening the file moves the positioned pointer within the file back to the beginning of the file, so that the entire file becomes available again for sequential referencing. These operations serve much the same function as a RESTORE statement would to the pool of DATA statement. 5-9 5.5 VIRTUAL DATA STORAGE Many applications require a capability to individually address and update records on a disk file in a random (non-sequential) manner. Other applications may require more core memory for data storage than is economically feasible. EDUBASIC fills both these requirements with its easy-to-use random access file system, called virtual core. The EDUCOMP BASIC virtual core system provides a mechanism for the programmer to specify that a particular data array is not to be stored in the computer's core memory, but within the OS/8 file system, instead. Data stored in files external to the user program will survive, even after the user leaves his terminal, and can be retrieved by name at a later session. Items within the file are individually addressable, as are items within core arrays. In fact, it is the similar way in which data are treated in both core and random-access files which leads to the name virtual core. The matrix format is used to store data because in a normal data file, described earlier, the PRINT and INPUT statements deal only with the next sequential data element. A normal data file, then, is much more limited in its applications and depends upon a strictly sequential treatment of I/O. With virtual data storage, the user can reference any element of the file, no matter where in the file it resides. This random access of data allows the user program to perform non-sequential referencing of the data for use in any BASIC statement (which is to say that the virtual core arrays need not be read into core to be available to the program for use) • 5.5.1 Virtual Core DIM Statement In order for an array of data to exist in virtual core, it must be declared in a special form of the DIM statement (places in program sequence somewhere after the corresponding OPEN state- 5-10 merit). This special statement is as follows: line ~umber DIM # expression ,. list where the expression is an integer constant between 1 and 4 and corresponds to the internal file designator on which the program has opened an internal file. The variable list appears as it would for a normal core resident array DIM statement. Thus, a 100 by 100 matrix could be defined as: l~ DIM #2, A(l~~ ,l~~) Numbers and strings can both reside in virtual core arrays. More than one array can be specified in one virtual core file. For example: 25 DIM #1, A(l~~~) , C$(25~~) which allocates space for 1000 numbers and 2500 character strings (15 characters long each). 5.5.2 Virtual Core String Storage One of the few differences in data handling between core and virtual arrays occurs in the storage of strings within string matrices in virtual core. Strings in virtual core are of fixed length (all elements having a particular name are of the same length.) This length can be defined by the programmer and varies from 1 character to 2000 characters. The system forces lengths to be a multiple of 3: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, If the user indicates other than one of these values, he will receive the next higher size. Thus: l~ DIM #1, X$(l~)= 65 is the same as: l~ DIM #1, X$(l~}= 66 If no lertgth is specified, a default length of 15 characters is assumed. The length attribute of virtual core strings (as well as ordinary strings) is specified in the DIM statement, using the notation: 5-11 15 DIM #1, A$(l~~) = 32, B$(1~~)=4, C${l~~) where A$ consists of 101 strings of 33 characters each; B$ consists of 101 strings of 6 characters each; C$ consists of 101 strings of 15 characters each; 5.5.3 0Eening a Virtual Core File In order for the user to reference his virtual core file, he must first associate one of his files (known by name) with an internal file designator from 1 to 4 (which is then used in the virtual DIM deqlaration). This is normally done with the following OPEN statement: line number OPEN string AS FILE expression where the string is the name of a file and the expression specifies an internal file designator; thus: 35 OPEN "PAY" AS FILE 1 associates the file named "PAY" with internal file 1. If "l?AY" already exists, then the existing file is used; if there is no file named "PAY" one would be created. The extension .BR is assumed. Sophisticated users are urged to read Chapter 8 ~Thich describes the system implementation of the virtual core processor. A mastering of this information will produce programs which utilize the system resources in a highly efficient manner. As an example of virtual core usage, consider the problems of implementing an information retrieval system for a small organization. There might be 1000 employees, each needing a 300-character record containing the name, home address, phone, work station, and phone extension of the employee. Rather than order the records in the file, to 5-12 it is decided maintain a separate index file containing only badge numbers. The order of employee. records in the master file is the same as the badge number sequence in the index fil~. Thus, to extract information on an employee with badge n, we find his badge number in the index file and use the index found to retrieve his data from the master file. Since the number of employees is small, numeric data can be used in the badge file; only alphanumeric data is stored in the master file. A program to print an employee's name, given his badge number, might appear as follows: LISTNH 5 DIM R$=300 1.0! PRO GRAM TO LOOK UP NAMES IN MASTER FILE IBADGE FILE 20 OPEN "BADGE" AS FILE 1 !MASTER FILE 30 OPEN' "MASTER" AS FILE 2 ! 1000 BADGE NUMBERS' 40 DIM OIl B(1000) 50 DIM 62i A$(1000)=300 11000 RECORDS I EACH 300 !CHARACTERS LONG 55 60 PRINT "INPUT BADGE NuMBER";: UJPUT E ! GET EMPLOYEE Nm-mER FOR!of TTY US BADGE II IN FILE? 70 FOR 1=1 TO 1000: IF B(I)=E THEN 100 75 NEXT I 80 PRINT "NO SUCH EMPLOYEE": GOm 60 !NO 100 nIE NOW, HAVE INDEX INTO FILEI I !YES 110 R$=A$(I) !BRING RECORD INTO CORE J20 PRINT "NAME IS";MID(R$11011S) ! NAME IS FROH COL UMN 10 TO 25 INEXT ••• '130 ,GOTO 60 200 END READY 5.6 CLOSE STATEMENT The CLOSE s~atement is used to terminate I/O to or from a device. ~nce a file has 'been closed, it can be reopened for reading or writing on any internal file designator. All files are automatically closed at the end of program execution. mat of the CLOSE statement is as follows: line number CLOSE 5-13 expression The for- Any number of files can be closed with a single CLOSE statement; if more than one, they are separated by commas. The expression indicated is the same expression used in the OPEN statement and indicates the internal file designator. By choosing a file with the CLOSE statement, the user frees more core storage space to open other files (a maximum of 12 depending upon the space available). For example: 255 CLOSE 2, 4 345 CLOSE 3 Line 255 above closes the files opened on internal device designators 2 and 4. Line 345 closes the file open on internal device designator 3. 5.7 KILL STATEMENT The KILL statement is of the form: line number KILL string and causes the file named string to be deleted from the user's file area. For example, when the user has completed all work with the file XYZ.DA on the system disk, he could remove the file from storage by executing the following statement: 455 KILL "XYZ.DA" When using the KILL statement, extensions must be used. Otherwise no error statement is given but the file is not deleted. 5.8 CHAIN STATEMENT If a user program is too large to be loaded into core and run in one operation, the user can segment the program into two or more separate programs. Such programs are called into core for execution by means of a CHAIN statement. Each program section is assigned a name and control can be transferred between any two programs. A CHAIN statement is of the form: 5-14 line number CHAIN string [line number] and causes the program named by the string to be called, comi1ed (if necessary), and executed. The line number, if specified, designates the line at which the program is to be started. If the line number is omitted, the program is started at the lowest numbered line (as though a RUN command had been used). The CHAIN statement is the last statement executed in each program segment other than the last segment. For example: 1~~~ CHAIN "MAIN'~ 2~~ . causes the program MAIN to be loaded and started at line 2~~. Chaining to precompiled program files (.BC files) is considerably more efficient than chaining to BASIC source program files since .BA files require compilation upon each call. Communication between chained programs is performed by means of the user's file area. If no extension is given, EDUBASIC looks for a .BC file. If no .BC file is found, EDUBASIC looks for a .BA file. If no .BA file is found, an error message results. When the CHAIN statement is executed, all open files for the current program are kept open, the new program segment is loaded, and execution continues. Virtual files should be closed and reopened across a chain. The significance of not having to close and reopen a sequential data file is that the file pointer will not be reset (see section 5.4). In other words, a PRINT statement in the chained-to program will add information to the end of the file. This significance is not present when working with virtual files. 5-15 CHAPTER 6 EDUBASIC GENERALIZED INPUT AND OUTPUT OPERATIONS 6.1 READ AND DATA STATEMENTS A READ statement is used to assign to a list of variables values obtained from a data pool composed of one or more DATA statements. The two statements are of the form: line number READ list of variables line number DATA list of values The list of variables c~n include numeric, subscripted, or character string variables. The list of values must correspond in type with the variables to which the value will be assigned, (although they are stored according to the type of the variable.) The data pool consists of all DATA statements in a program. Values are read starting with the DATA statement having the lowest line number and continuing to the next higher, etc. The location of DATA statements in a program is irrelevant, although for simplicity they are usually kept together toward the end of the program. (The DATA statements must occur in the proper numeric sequence, however.) A DATA statement must be the only statement on a line, although a READ statement can occur anywhere on a line. Comments are not permitted at the end of a DATA statement. If a READ statement is unable to obtain further data from the data pool, an error message is printed and program execution is terminated. Quotes are necessary in DATA statements only around string items which contain a comma or where leading ~e significant. blanks within the string The data pool, composed of values from the 6-1 program's DATA statements, is stored internally as an ASCII string list. When a numeric variable is read, the appropriate ASCII to numeric conversions are performed. When a string variable is read, the string is used as it appears in the DATA statement. If the item did not appear in quotes; leading spaces are ignored. If the item did appear in quotes, the string variable is equated to the entire string within the quotes. 6.'2 RESTORE STATEMENT The RESTORE statement reinitializes the data pool of the program's DATA statements. This makes it possible to recycle through the DATA statements beginning with the lowest numbered DATA statement. The RESTORE statement is of the form: line number RESTORE For example: 85 RESTORE causes the next READ statement following line 85 to begin reading data from the first DATA statement in the program, regardless of where the last data value was found. See Section 3.3.1 for an'example program using the RESTORE statement. The RESTORE statement can be placed in any position on a multiple statement line. 6.3 INPUT STATEMENT The INPUT statement allows data to be entered to a running program from an external device, the user's keyboard, disk, DECtape, paper tape reader, etc. The full form for this state- ment is: li'ne number INPUT[:fI: expression,] variable list In many cases the simpler form: line number INPUT is used. variable list This last form causes a ? to be printed at the ter- minal and the system then waits for the user to respond with the appropriate values. If sufficient values are not typed, the system prints another ?i if too many values are typed, excess values are ignored. The format: line number INPUT # expression, variable list causes input to be read from the file or device indicated, in the expression, by the internal file designation number given when the file was opened. If the value of the expression is non-zero and the specified file is open to the user terminal as an input device, then no ? character is printed at the terminal when input is requested. For example: 75 OPEN "TTY:" FOR INPUT AS FILE 2 816 INPUT #2,A The system then pauses while the user types a numeric value for the variable A, although no prompting ? or character string message is printed on the terminal. Another format of the INPUT statement allows for the entering of an entire line of data as a single character string entity, regardless of punctuation. This statement is different from the normal mode of string input, where the comma and double quote characters have special significance. The format is: line number INPUT LINE[# expression ,] string variable For example, the statement 25 INPUT LINE A$ 6-3 / would print a question mark and wait for the user to enter a line followed by the RETURN. As another example: 2f1 OPEN IF2" FOR INPUT AS FILE 4 25 INPUT LINE #4, B$ These lines cause the system to open a file F2 on the system disk on channel #4 (of 4 possible channels) to input a line of characters up to the next RETURN character. 6.4 PRINT STATEMENT In its simplest form, the PRINT statement: lirie number PRINT causes a carriage return/line feed to be performed on the user terminal. The format: line numb~r PRINT list causes the printing of the elements in the list on the user terminal. An element in the list can be any legal expression. When an element is not a simple variable or constant, the expression is evaluated before a value is printed. The list can also contain character strings between quotes which are printed exactly as typed between quotes. Elements in the list are separated by commas or semicolons. For example: l~ A=l: B=2: C=3 15 PRINT A; A+B+C, C"-A, "END" when executed causes the following line to be printed: 1 6 2 END A terminal line is considered to be divided into five l print zones of fourteen spaces each. Use of these zones involves lThe actual number of print zones is INT (n/l4), where n is the size of the print line. 6-4 the comma character which causes the print head to move to the next available print zone (from 1 to 14 spaces away). fifth print zone on a line is filled, the pri~t If the head moves to the first print zone on the next line. The.semicolon character functions as follows: a. if a numeric variable or expression is followed by a semicolon, the value is printed with a preceding minus sign if the number is negative, or a preceding space if it is positive. The number is then followed by a single space. b. character strings and string variables followed by a semicolon are printed with no preceding or trailing spaces .. Any PRINT statement which does not end with a semicolon or comma character causes a skip to the next line after printing the elements in the list. The presence of the punctuation character at the end of the PRINT list causes the next PRINT statement to continue on the same line under the conditions already defined. In general, the output rules for the PRINT statement are a. suppression of leading and trailing zeros to the right of a decimal point. Where a number can be represented as an integer, printing of the decimal point is also suppressed. b. at most six significant digits are printed. c. most numbers are printed in decimal format. Numbers too large or too small to be printed in decimal format are printed in exponential format. d. character string constants are printed without leading or trailing spaces. e. extra commas cause print zones to be skipped. Output can be directed to a device other than the user terminal with the following command: 6-5 line number PRINT # expression , list The expression is the number of a previously opened output file. For example: l~ OPEN "PTP:" FOR OUTPUT AS FILE 3 5~ PRINT #3, B,D,A+7,FNX(B) causes four values to be punched onto paper tape by the high speed punch which is opened for output as file 3, of 4 possible files. As many as four possible virtual files may be open at once (for input or output). 6.4.1 PRINT-USING Statement In order to perform formatted output, the following statement is used: line number PRIN~[# expression ,lUSING .string , list where the expression (which is optional) indicates the file or device which is the destination of the output; the string is either a string constant, string variable, or string expression which is an exact image of the line to be printed; and the list is a list of items to be printed. All characters in the string are printed as they appear except for the special formatting characters and character combinations described on the following pages... The string, or portions of the string, are repeated until the list is exhausted. The string is con- structed according to the following rules: Exclamation Point An exclamation point identifies a one character string field. The string is specified in the list within the PRINT statement. For example: l~ PRINT USING "!!!", "AB ", "CD", liEF" which causes: ACE 6-6 to be printed at the user's terminal. The first character from each of the three string constants or variables is printed. Any other characters beyond the first are ignored. String Field A variable string field of two or more characters is indicated by spaces enclosed between backslashes. ~) The backs lash character is produced by typing SHIFT/L on the Teletype keyboard. Enclosing no spaces indicates a field two columns wide, etc. For example: 2~ PRINT USING "\\\ \ ", "ABeD", "EFGHI" causes ABEFGH to be printed at the user's terminal. The first two backslashes have no spaces enclosed, hence permit the printing of two characters (AB). The second two backslashes enclose two spaces and permit the printing of four characters (EFGH). No spaces are printed unless specifically planned. Numeric Field Numeric fields are indicated with the # character. Any decimal point arrangement can be specified and rounding is performed as necessary (not truncation). 3~ For example: PRINT USING "###.##", 12.346' causes 12.35 to be printed on the user's terminal, while 4~ PRINT USING "####", 12.345 5~ PRINT USING "####.", 12.345 6~ PRINT US'ING "##", l~~ causes 12 12. * to be printed on the user's terminal. Numeric fields are right justified; that is, if a number does not fill the allotted 6-7 space, leading blanks precede the number. When the field specified is too small for a constant or variable to be printed, an asterisk is printed for each alloted space. If the format field specifies a digit as preceding the decimal point, at least one digit is always output before the decimal point. If necessary, that digit is zero. Exponential Format When the exponential form of a number is desired, the numeric . field is followed by the string tttt (four t characters) which allocates space for E-xx. Any arrangement of decimal points is permitted. For example: 5 F$="llltttt ilill#" 19' A=l~J:rf1f1. 2~ PRINT USING F$,A,A causes 19'E + 9'3 lJ:rJ:r~~ to be printed at the user's terminal. All format positions are used to output a number with an exponent. The significant digits are left justified and the exponent is adjusted. PRINT Statement Punctuation When the PRINT-USING statement is used, the usual PRINT statement punctuation characters (commas and semicolons) have no effect on the output format, except that a semicolon at the end of the PRINT list does inhibit termination of the printed line. II II", 1;2,3 l~ PRINT USING "II prints the following: 123 6-8 As another example: l ' PRINT USING """#.##", 2;5: 2' PRINT "X" prints 2.5,X As another example: 1, LET A=1.321l1: B=2.45457 15 LET F$ = "A=##.##B=##.##" 2' OPEN "LPT:" FOR OUTPUT AS FILE 4 25 PRINT #4, USING F$, A,B would cause: A= ; /32B= 2.A5 to be printed on the line printer. 6.4.2 PRINT Functions In order to aid in formatting simple and complex PRINT statements the following functions are provided: Meaning Function POS(X) Returns the current position on the output line; where X is the I/O channel number. POS(~) returns the value for the user's terminal. TAB (X) Tab to position X in the print record. For example, a standard Teletype has 72 printable columns numbered ¢ through 71. TAB (4) causes sufficient spaces to be output to move the print head to column 4. If the print head is currently past position 4, no spaces are output. For example: 1, PRINT "X";TAB(l¢);POS(¢) causes the following to be printed: Xl ~ 1[1 position i ~ position 1[1 6-9 CHAPTER 7 EDUBASIC COMMANDS 7.1 INTRODUCTION We have discussed the st~tements in EDUCOMP BASIC which are available to the programmer to solve the problem. However, equally important are the commands or immediately executed key words in BASIC which permit you to perform the tasks of creating your program, debugging it, running the program, and finally, saving the statements. All of these steps are greatly eased with the rich vocabulary of commands in EDUCOMP BASIC. The user is assumed to be familiar with OS/8 and how to start up an OS/8 system. In response to the dot (.) given by the OS/8 command decoder, type .R BASIC EDUCOMP BASIC responds with READY 7.2 CREATING A PROGRAM In order to create a new user program, at any time a user can issue the NEW command as follows: NEW followed by the RETURN key. The system responds by printing: NEW FILE NAME-- 7-1 to which the user responds by typing the name of the new program (no more than six characters). When typing a new BASIC program, the file name extension .BA (for BASIC) is added to the name by the system. Alternatively, the user can give the command NEW followed by the program name, to avoid having the system prompt the typing of the program name: NEW CALPPB is equivalent to NEW NEW FILE NAME--CALPPB When the NEW command is given, it: a. Deletes any program currently in core, and b. Causes BASIC to remember the new program name. NEW DTAI:CALPPB is meaningless. All checking for duplicate files occurs when the SAVE command is given. Following the creation of a new file with an acceptable file name, the user can begin to type his program, beginning each line with a line number. If the user doesn't type NEW either he will get the program name given to the previous program or BASIC will create a file called NONE (if no previous name has been given) which can be referenced later as NONE. be changed (see section 7.5). At any time, this name can Only one file with the name NONE can exist at anyone time. 7-2 7.3 CALLING AN EXISTING PROGRAM When the user desires to recall the source file of an old BASIC program (previously saved on a storage device), he gives the OLD command as follows: OLD to which the system replies: OLD FILE NAME-The user then types the name of the old BASIC. file containing the program. Alternatively, the user can indicate the old file name without prompting, as follows: OLD TAXES which calls the old file TAXES from the disk. If the file is not available on the disk or if it is protected against that user, an appropriate message is printed. There is a more general form for the OLD command which allows the user to specify the particular OS/8 device on which the OLD program exists. OLD device:file name.extension If the program ALUM is to be called from DECtape number 1, the command string is OLD DTAI : ALUM where the extension .BA is assumed. OLD may also be used to read in a program (or data file) 7-3 from a non-file structioned device (TTY:, PTR:, CDR:, etc.). In this case, only the device is specified since these devices have no directory and do not store more than one file at a time. As an example, OLD CDR: reads a program from the card reader. NOTE: When accepting input from non-file structional devices, CTRL/Z is used as an end-of-file character. This character may be typed at the console or may occur at the end of the file on the particular device used. OS/8 automatically inserts a CTRL/Z at the end of a paper tape reader file. 7.3.1 CALLING DATA FILES A further generalization of the OLD command occurs in EDUBASIC for use with data files. Certainly a data file may be called into memory with the previously described versions of the OLD command. However, many times it is very convenient to be able to append line numbers to the elements in a data file to ease editing the information. OLD The full form of the OLD command is device:file name.extension line number, increment As an example, OLD RKA2:STUDNO.DA 100, 5 brings in the data file STUDNO from the second RK8e disk and numbers each element starting with line number 100 in increments of 5. New elements may be added, deleted, or modified 7-4 easily and the file may be stored again without the line numbers by using the NSAVE command (section 7.5). 7.3.2 OVERLAYING A PROGRAM Sometimes it is necessary to append a subroutine or series of statements to an already existing program. The OVERLAY com- mand works exactly like OLD except that the program already in memory is not destroyed. OVERLAY deviceifile name.extension line number, increment As an example, file BX on SYS contains 49 59 PRINT "TELL ME AGAIN" GO TO 1.0' The program (LOVE) in memory is III 29 311 611 PRINT "IF YOU LOVE ME, TYPE A 7" INPUT A IF A #7 THEN PRINT "I DON'T LOVE YOU EITHER": GO TO END 6~ If the command is now given, OVERLAY BX BX now contains lines 1.0' through 6.0'. LISTNH lJa' 2[0 39 4Ja' 5fO 69 PRINT "IF YOU LOVE ME, TYPE A 7" INPUT A IF A #7 THEN PRINT "I DON'T LOVE YOU EITHER": GO TO 6[0 PRINT "TELL ME AGAIN" GO TO 1[0 END The OVERLAY command is very useful for adding a subroutine to a program. Both the OLD and OVERLAY commands may be used only to call 7-5 ASCII files into memory (e.g., not compiled or .BC files). Any file called with OLD or OVERLAY may be edited by the user at the terminal. 7.4 EDITING PROGRAMS During the course of typing a program at the terminal or after a program is seen to be incorrect, changes can be made in the text of a program. These changes are made in what is called the editing phase of BASIC, between the time when the system prints READY and the time when the user types RUN. (During this time, commands can be executed.) The simplest type of correction is done during the typing of a line before the line is entered to the system with the RETURN key. For example: l~ PRHNT If the user realizes he has typed PRH instead of PRI, he can type the RUBOUT key once for each character to be erased. The RUBOUT key causes the erased character to be echoed on the user terminal between back slashes as they are erased. . example: For ABC<RUBOUT> <RUBOUT>DEF Typing the above is printed on the terminal as follows: ABC\CB\DEF If the RETURN key is typed at the end of the above line, the system would receive it as follows: ADEF The letters Band C have been erased. 7-6 If the user decides that his ~ntire easi~st course is to delete the line, and he has not yet typed the RETURN key, then he can type CTRL/U (hold down CTRL and U keys), which performs this function. If the RETURN key has been typed, then the line may merely be retyped; the second version will replace the first in the computer memory. 7.4.1 THE EDIT COMMAND One of the most useful commands in EDUBASIC is the EDIT command. This search command permits the user to modify a com- pleted line or statement which is already contained within the memory of the computer. Thus, EDIT should be contrasted with the use of the RUBOUT key where the latter is used for changing a line already completed, i.e., RETURN has not been typed. EDIT tells the computer to find a given line number, and to th'en search for a particular in that line. As an example, READY EDIT 12.0 (character) When you type the character to be searched for, this character is not printed, but the line requested is immediately printed out to the character which you have typed. If there are sev- eral occurrences of this character, the first one is printed and printing ceases. At this point you have several options: a. Type a RUBOUT to delete the last character printed; type two RUBOUTs to delete the last two characters printed; and so on. b. Type in new characters to take the place of any you rubbed out; or, of you have not typed any RUBOUTs, to add to the text already there. c. Type CTRL/L; the computer will now search for the next occurrence of the same search character. 7-7 7.4.3 DELETE COMMAND The DELETE command is used to remove one or more lines from the user program currently in core. DELETE For example: l~~ causes line number 100 to be deleted. (The user should first be certain that no other line references line number 100 unless that line is to be replaced.) DELETE 1~~-2~~ causes all the program lines between and including line numbers 100 and 200 to be deleted. If 100 and/or 200 do not exist in the program, any lines within the range from 100 to 200 are deleted. If several groups of lines are to be deleted, then the user can type: DELETE 1~~-2~~, 3~~-4~~, l~~~-ll~~, l62~ which deletes all lines between 100 and 200, 300 and 400, 1000 and 1100, and line number 1620. Individual lines may be deleted with the following form: DELETE l~, 33, 976 This command deletes only lines 10, 33 and 976. If only one line is to be deleted it may be more convenient merely to type the line number and the RETURN KEY: which is equivalent to: DELETE l~ d. Type a BELL code (CTRL/G)i now type a new search character (which is not printed). The computer will now print out the line until it meets this new character. e. Type the ALT MODE keYi the left half of the line, up to and including the last character printed, is erased. The line number, however, is not erased. f. Type the RETURN key. All the line to the right of the last character printed is dropped. The left side of the line is saved and the RETURN indicates that the EDIT command is complete. g. Type the LINE FEED key. The whole line, in its present condition (including any changes you have made) will be printed but not saved. To save the line you must type RETURN.-:LINE FEED may be typed as many times as you like. Note that EDIT cannot be used to change a line number. The only way to move a line to a new position in the program is to retype it, complete with its new line number. should then be deleted. The old line The RESEQUENCE command (next section) is useful for creating the space to add new lines. 7.4.2 THE RESEQUENCE COMMAND The RESEQUENCE command simply renumbers the line numbers in the user program. The general form of this command is RESEQUENCE line number, increment If only the word RESEQUENCE is typed and no line number and increment are specified, the program is renumbered starting with line number 100 in'increments of 10. Note that only the program (or data file) currently in memory is resequenced and that if you wish to SAVE the new version or REPLACE the old version, these commands must be given (see section 7.5). 7-8 ~IST Command Meaning LIST List the entire user program as it currently exists. NLIST Same as LIST, but without line numbers. LISTNH Same as LIST, but without a program header. NLISTNH Same as NLIST, but without a program header. LIST n List line n, without a program header. LIST m,n,p List lines m,n,p without a program header. NLIST m,n,p List lines m,n,p without line numbers. LIST n1-n2 List lines n1 through n2, inclusive, without a program header. LIST LPT; Lists the user program on the line printer (if one exists on the system). 7.4.5 SEARCH One of the most powerful editing features in EDUBASIC is the SEARCH command. The first form is SEARCH nl-n2/string A/ This SEARCH command lists all lines in the range nl to n2 inclusive that contain string A anywhere in the line. If no line numbers are specified, the entire text buffer is searched. Note that string A may be a variable name (A$) or a group of characters (ABCD) without quotation marks unless the quote marks are part of the string. The second form of the SEARCH command is SEAR~H nl-n2 /string A/string B/list This form of the SEARCH command replaces all occurrences of string A with string B in the range nl-n2. If the optional word list is specified at the end of the command, all line numbers in which replacement was performed are listed. If no line numbers are specified, the entire text buffer is searched. 7-11 7.4.4 LIST COMMAND . The LIST command is used to obtain a clean printed copy of all or part of the user's current program. This listing is especially useful during and after an editing session in which the original program is changed. In order to obtain a printed copy of the entire program as it currently exists within the system, type: LIST In order to list a single line, type: LIST lfAfA to type line 100. (LIST 100, 300 lists both lines 100 and 300.) ". In order to list a section of the program, type: LIST lfAfA-2fAfA which will cause the listing of the entire program from line number 100 to line number 200 inclusive. The above LIST commands list both statements and line numbers. If the user wishes a listing without line numbers, the command NLIST is available. NLIST may be used similarly to the three cases above, but the lines listed will have no line numbers. In the first of the above cases, BASIC prints a program header containing the program title and data. If this header is not desired (as it might not be for normal editing), the command may be given as LISTNH to delete the header material. marize: 7-10 To sum- EXAMPLE: File in memory contains: l~ 2~ 3~ 4~ S~ 6~ 7Y1 PRINT "SEARCH COMMAND USAGE" INPUT B IF B=S THEN 2~ B=B+l PRINT B GO TO 2Y1 END SEARCH 30-60/20/ 3Y1 6Y1 SEARCH 2Y1 /B/C/LIST 2Y1 3Y1 4Y1 SY1 SEARCH IF B= S THEN 2Y1 GO TO INPUT C IF C=S THEN 2Y1 C=C+l PRINT C PRINT/PRINT Bi/ The file in memory now contains: l~ 2~ 3Y1 4~ SY1 6Y1 7~ PRINT Bi "SEARCH COMMAND USAGE" INPUT C . IF C=S THEN 20 C=C+l PRINT C GO TO 2Y1 END (In order to permit the slash (/) to be part of the string, an alternate form of the SEARCH command allows replacement of the slash by any non-numeric character -- e.g., SEARCH A/A*A replaces all slashes with asterisks.) 7.S MANIPULATING USER PROGRAMS The commands in this section enable the user to compile, save, . run, and rename his files. These are all operations performed on a program as a whole (either in core or as a file) and are used once a complete program has been prepared at the terminal. 7.S.1 RUN Command The RUN command is used to cause the execution of any source 7-12 BASIC program. (Source programs are stored as the user typed ,them; compiled programs are files described in section 7.5.2.) In order to run the program currently in core, the user simply types: RUN This command causes the execution of the program in core. A program header is printed after the RUN command is given, consisting of the program name, date and language. If this in- formation is not desired, the command RUNNH should be given. RUNNH executes the current program without printing the header material. 7.5.2 EXECUTE Command" When it is desired to run a program not in memory, the EXECUTE command is used. EXECUTE device:file name.extension line number This command causes BASIC to search for file name on the device, load it, compile it (if necessary), and run it if it is found. If no extension is specified and both the .BA (source) and .BC (compiled) versions exist, BASIC will execute the compiled form because it requires less time. In order to retrieve and execute the source, it is necessary to specify the extension .BA after the file name. An alternate approach is to give the OLD command followed by the RUN command. This approach is not equivalent to the EXECUTE command because EXECUTE will save the file currently in core (before EXECUTE is typed), execute the program called for, and then restore the previous file into memory. 7-13 Compiled (.BC) files can only be executed with the EXECUTE Command. If only the source version of a file exists on a device, the EXECUTE command serves as a combination of the OLD and RUN commands, except with the restoring of the previous file noted above. For example, if the program STOCK is stored on DECtape 1, it may be called into memory and executed with the following single command string: EXECUTE DTA1:STOCK, l~~ where execution starts at line number 100. (Perhaps lines 1 through 99 contained instructions not required for the running of the program.) EXECUTE As another example, CDR: reads a BASIC program from the card reader and runs it. 7.5.3 SAVE Command The SAVE command is used to store BASIC source programs on the disk as follows: SAVE The program currently in core is saved under its file name with the extension .BA. If a file of the same name exists, then SAVE returns the error message: DUPLICATE FILE NAME Where the current name of the file is not the desired name, the format: SAVE GRADE 7-14 can be used, which saves the program currently in memory under the name GRADE.BA. In cases where the desired storage device is not the default device, the format: SAVE device:fiie name.extension ni,n2-n3,n4 is used where device indicates the device designation. file is stored as FILE NAME.BA. SAVE The For example: DTA4:ACCPAY saves the whole file ACCPAY.BA on DECtape 4. The numbers (ni, n2-n3,n4) are used if only part of the file in memory currently is to be saved. As an example: SAVE DTA4:ACCPAY l~, 1~~-36~ saves only lines 10 and 100 through 360 of the file ACCPAY on DTA4. The SAVE command is used only with source files and cannot be used with compiled files. When a program is saved, under some name, the program is still in core to be used or ignored as the user wishes. To obtain a listing of his program on the line printer, the user can type: SAVE LPT: . To punch a tape of his program, the user can type: SAVE PTP: 7-15 7.5.4 SAVE Without Line Numbers The NSAVE command saves the file currently in memory but without line numbers. NSAVE device:file name nl,n2-n3,n4 This particular command is very useful during the editing of a data file. The file may be called into memory with the OLD command and at the same time line numbers may be appended. OLD DTAl:PARTFL l~~,l~ After editing has occurred (adding, deleting, or changing the items in the file), the NSAVE command is used to save the file without line numbers. NSAVE DTA2:PART2 1~~-95~ The above command string saves only lines 100 through 950 of the new data file (PART 2) on DTA 2. 7.5.5 UNSAVE Command The UNSAVE command is used to remove a file from a storage device. The form: UNSAVE device:file name.extension removes the file name from the device. Any number of files may be .removed. Each name must be sepa- 'rated from the following name by commas. UNSAVE As an example PARTl, PART 2 , PART3 If no extension is given .BA is assumed. 7-16 If no file name is given, BASIC responds wit~ FILE ~AME ~- and waits for the user to input a file name. 7.5.6 RENAME Command The RENAME command causes the name of the program currently in core to be changed to the specified name. RENAME For example: COLGNO The old name of the program in core is discarded and it is now known as COLGNO. If the SAVE command is given: SAVE the file COLGNO.BA would be stored on the systems device. 7.5.7 REPLACE and NREPLACE The REPLACE command is used when the program in memory has the same name as a file on the same device and the user wishes the program in memory to become the new file with that name. The command is simply of the form: REPLACE device:file name.extension nl,n2-n3 where nl,n2-n3 indicate that only these lines may be saved. REPLACE is/ like SAVE, but destroys without notice the old copy of the same file, if it exists. NREPLACE is the same as REPLACE except that the file is saved 'without line numbers. 7.5.8 COMPILE Command Normally BASIC reads each line of a user's program as it is typed and, if acceptable, translates the line into a form 7-17 more easily understood by the computer. When lines within the user's program are altered"al,;L _lines which are in the program need to be recompiled (i.e., translated). . When the SAVE command is given, only the source version of the program (i.e., the text that is typed in response to the LIST command) is retained in the specified place. In response to the OLD command, BASIC reads the text from a file and compiles it in much the same manner as is done when the program is read from the user's keyboard. Once a program is completely developed and debugged, it may be desirable to avoid the time-consuming practice of compiling the program every time it is fetched from the library. this reason, the COMPILE command has been provided. For This command permits the user to save an image of his compiled program, rather than (or in addition to) the source text of the program. This compiled program may be called and executed with a minimum of overhead by use of the EXECUTE command (see section 7.5.2). Due to the transformation which takes place when a program is compiled, a file with the extension .BC can only be executed, it cannot be edited. Therefore, the user can issue the EXECUTE command with respect to these compiled files, but the file cannot be brought into core with the OLD command. If the current file name (i.e., that which is typed in the heading of a listing) is INVCTL, then the command COMPILE will save the compiled program in a file named INVCTL.BC. If another name is desired for the compiled file, it may be specified. COMPILE INVCL4 will generate a file named INVCL4.BC while the source file 7-18 in the above example will be saved as INVCTL.BA. 7.6 LENGTH COMMAND The LENGTH command returns the length of the user's current program in memory. For example: LENGTH 710 CHARACTERS (2 BLOCKS) The LENGTH command may also be used to give the length of lines in a program, by specifying the line numbers after the work LENGTH. As an example, LENGTH 1~f1-2~f1 354 CHARACTERS (1 BLOCK) The maximum size of a program to be run depends upon the number of variables in the program as well as the amount of text. This size varies between about 13 and 18 blocks. An 18 block file will not always execute, but may be edited. 7.7 CATALOG COMMAND Giving the CATALOG command causes the user's file directory to be printed on the console. For example: CATALOG PPB .BA 4 + + + name extension size 3/29/71 + creation date To obtain a CATALOG of files on a device other than the systems device, one can give the command CATALOG DEV: For example: CATALOG DTA4: 7-19 lists the files on DECtape unit 4. 7.8 COMMANDS FOR INPUT/OUTPUT DEVICES EDUBASIC has several commands specifically for I/O. However, it should be remembered that OS/8 handles all I/O (except the console) for EDUBASIC and many system commands should be given while under the monitor (e.g., ASSIGN). 7.8.1 TAPE COMMAND The TAPE command is used to disable the terminal echo feature when reading a paper tape with the low-speed (terminal) reader. The command is given as follows: TAPE {initial line number, step} EDUBASIC will add line numbers to a file if no line numbers exist on the tape (especially data files). The tape is in- serted in the low-speed reader and the reader control switch set to START. Prior to giving the TAPE command, the user would set up conditions such that the system expects the program. not scratch memory. TAPE does For example, giving the following com- mands: NEW ADDREC TAPE causes the system to await the new program file ADDREC which is to be entered to the system via the terminal tape reader. Giving the TAPE command disables the echo feature so that the program is not listed on the terminal as it is read. same function would be served by the following commands: OLD ADDREC TAPE 7-20 The 7.8.3 PUNCH and NPUNCH It is sometimes necessary to produce a paper tape using the low speed punch on the console teletype. The PUNCH (and NPUNCH) is used to create this tape. PUNCH nl,n2-n3 NPUNCH nl,n2-n3 These commands punch a copy of the file currently in memory; the latter command produces no line numbers. The line num- bers may be used to indicate which lines are to be punched. The user types the word PUNCH, types a carriage return, and turns on the paper tape punch. Typing LISTNH, turning on the paper tape punch, and then typing a RETURN accomplishes approximately the same result, with the exception that leader is not punched and READY is punched after the program has been punched. PUNCH also punches the program name, extension, and date on the paper tape, which may be read by the user. Note that when reading in a tape, the name, extension, and date punched by PUNCH should not read in. the reader after this information. 7-21 Place the tape in 7.8.4 MARGIN COMMAND The maximum line length can be changed using the margin command. The margin command is of the form: MARGIN number The above statement changes the line length on all output devices from 72 to the specified number. MARGIN is in ef- fect until another MARGIN command is given. Even leaving BASIC and then calling it in again or re-bootstrapping will not change the margin back to 72. The maximum line number is in effect for all commands and all output devices. It is not in effect for character string out- put. The following description will help the user to more fully understand the MARGIN command. The user may continue typing his line of text or command until the specified margin is reached. At this point an automatic Carriage RETURN/LINE FEED is performed and the user is allowed to keep typing. Only by striking the RETURN key does the user enter his command. No commands are affected by the margin command, i.e., operation of BASIC is the same with a line of 72 characters or a line of 5 characters. Even though a single statement may be printed as 10 lines with the new margin, those 10 lines are considered as 1 line to the computer. 7.9 SPECIAL CONTROL CHARACTERS Some characters previously discussed are reviewed here. Addi- tional control characters are available from OS/8. 7.9.1 RETURN KEY Typing the RETURN key echoes as a carriage return/line feed 7-22 operation on the terminal, as long as the terminal is not in TAPE mode. ~he RETURN is used to indicate the end of a line typed. RUBOUT key is of no use for corrections on the line just typed after the RETURN is typed. The line has been entered into the 'source' buffer. 7 .,9 • 2 LINE FEED KEY The LINE FEED key causes the current line to be echoed, free of rubouts, up to the point at which the user typed LINE FEED. A RETURN must be typed to cause execution (command) or enter the line (BASIC statement). 199 199 As an example, PRNT\TN\INT "MY NME\EM\AME IS LAH\HAL\HAL" (LINE FEED) PRINT "MY NAME IS HAL" where the carriage position (next position to be printed) is after the last quotation mark. Additional characters-may be added to the line or the RETURN key may be typed to accept the line as is. 7.9.3 RUBOUT KEY . The RUBOUT key is used as an eraser for the current line. If typed in TAPE mode, the RUBOUT key is ignored; otherwise, it causes the character most recently typed to be deleted. The erased characters are shown on the terminal paper between back slashes. 19 For example, LEF X=X*X could be corrected by typing the RUBOUT key 7 times (to remove the F) and typing the remainder of the line correctly. line would look as follows on the terminal 19 LEF X=X*X\X*X=X F\T X=X*X 7-23 pap~r: The and would appear to the system as: l~ LET X=X*X In cases where the mistake is toward the beginning of a line, it may be easier to simply retype the entire line. For exam- ple, l~ l~ LEF X=X*X LET X=X*X Once the second line is entered into the system, the first line numbered 10 is deleted. 7.9.4 CTRL/C CTRL/C returns control to the OS/8 keyboard monitor. BASIC may be recalled by typing R BASIC in response to the dot given by OS/8. START may also be typed and in most cases returns the user to EDUBASIC and into the program upon which he was working before CTRL/C was typed. 7.9.5 CTRL/P By typing a CTRL/P (hold down the CTRL key and type the P key, release both), the user causes BASIC to return to command mode, where commands can be given or editing done. CTRL/P stops whatever BASIC was doing at the time and returns control of the system to the user. 7.9.6 CTRL/U The CTRL/U combination deletes the current input line. This combination is useful when a long command has been typed and is no longer wanted. Rather than use the RUBOUT key repeatedly, CTRL/U cancels the entire line. This feature can be used when typing either commands or statements. line is deleted. 7-24 The entire physical 7.9.7 ~he CTRL/O CTRL/O combination suppresses output on the Teletype until the next time CTRL/O is typed (or CTRL/P is typed). When a program produces a large amount of output (usually in tabular form), the user may not wish to wait for the pr~nting of the complete information. CTRL/O enables the user to monitor the output while not stopping it completely. Typing CTRL/O while output is occurring still allows the computer to output the data, but the Teletype does not print it. This speeds up the output process, since the Teletype is a rather slow device. The second time CTRL/O is typed, the output is again sent to the Teletype for as long as the user wishes. CTRL/P, on the other hand, will completely stop the output. Think of CTRL/O as a switch, the first setting of which creates a condition and the second setting releases the condition. 7.9.8 TAB CHARACTER The TAB character or CTRL/I combination allows the user to insert a tabular format into his typed material. When entering a program to the system, the TAB character allows formatting. The BASIC editor considers each line as being broken into tab stops eight spaces apart across the line. Typing the TAB character causes the printing head to move to the next of those stops on the line. If using a model 33 Teletype, the TAB echoes as spaces. The model 35 Teletype has built-in hardware tabs. 7.9.9 CTRL/Z The CTRL/Z combination is used to mark the end of a file; when inputting data from a file, a CTRL/Z character marks the end of the recorded data. 7-25 CHAPTER 8 DETAILS OF VIRTUAL ARRAYS 8.1 INTRODUCTION The virtual array facility provides the means for an EDUCOMP BASIC program to operate on data structures that are too large to be accommodated in memory at one time. To accom- plish this, BASIC uses the disk or DECtape file system for storage of data arrays, and only maintains portions of these files in memory at any given time. An essential difference between real arrays and their virtual counterparts is the order in which array elements are referenced. In real arrays, the referencing algorithm has no effect on the time it takes to accomplish the references; while for virtual arrays, this order can have a significant effect on the program execution time. This chapter gives the user an in-depth look at the algorithms used in the virtual array processor, in order that users concerned with efficiency can optimize their use of this facility. Each DECtape or disk file appears to the user program as a contiguous sequence of 256-word records. Any position in a file can be specified internally with a two-component address; the first part being the relative record within the file, and the second being the position of the item within the block. One of the functions of the virtual array processor is to transform, or map, each virtual array reference into its corresponding file address. This virtual array processor is invisible to the user and BASIC performs all mapping functions automatically. Virtual arrays are stored as unformatted binary data. 8-1 This format means that no I/O conversions (internal form-to-ASCII) need be performed in storing or retrieving elements in virtual ·storage. Thus, there is no loss of precision in these arrays, and no time wasted performing conversions. All references to virtual arrays are ultimately located via file addresses relative to the start of the file. No symbolic information concerning array names, dimensions, or data types ------ is stored wi thin the file. differen~ray - names to refer to the data--------contained within a ... _._. __ .__ .. _...... -- .._,--....~--,. ....... ~------'----'-"'----'-'-'--'~"-' single virtual array file. ---. Thus, different progra:ns may ~e_. -----_. -----,,--:-- ..... , The user must be cautious in such operations, since it is his responsibility to ensure that all programs referencing a given set of virtual arrays are referencing the same data. Consider ~he following example: Program ONE contains 1~ OPEN "FILE" AS FILE 1 2~ DIM.il,X(l,) ,Y(lJ) Program TWO contains l~ OPEN "FILE" AS FILE 1 2~ DIM il,Z(l,) ,X(lJ) Whenever program TWO references the array Z, it is using the data known to program ONE as array X. Both' X and Z are the first arrays in their declarations, both contain numeric data, and both are 11 elements (X(J) , .•• ,X(l,» long. These two arrays, then, correspond in position, type, and dimension. References to the array X (in ONE) and to the array X (in TWO) do not refer to the same data, even though both are using the same virtual file (FILE). The concept of using relative posi- tion, rather than name, to identify data items is familiar to users of the FORTRAN COMMON facility. 8-2 Within a single EDUBASIC program it is possible to redefine a single virtual array file on the same channel for the purpose of reallocating the data within the file. 145 15~ 155 For example: OPEN "DATA" FOR INPUT AS FILE 1 DIM #1, A$(1~)=4 DIM #1, B$(4)=16 The program now has access to the file DATA through both the array A$ and the array B$. elements of A$ (B$(~) through A$(3), etc.). Each element of B$ contains four is equivalent to the elements A$(~) Note that the file is open for input only and that the two DIM statements reference that file on a single channel number (#1 in this case). Note also that the two statements: 75 8~ DIM #1, DIM #1, A(l~) B(l~) are not equivalent to the statement: 9~ DIM #1, A(l~) ,B(l~) In the first case the arrays A and B are equivalent to each other and constitute the first array in the file open on channell. In the second case the arrays A and B are defined as both existing in the file open on channell. CAUTION The user is advised not to open a single file under two different channel numbers. For example: 5~ 55 l~~ 1~5 OPEN "VALUES" AS FILE 1 OPEN "VALUES" AS FILE 2 DIM #1, DIM #2, X$(2~) Y$(2~) 8-3 causes two buffers to be created for the storage of input to/from channel 1 and to/from channel 2. Data output to channellis not available to channel 2, etc. 8.2 ARRAY STORAGE Numbers (floating point) are stored in four words (8 characters) in virtual files so that an integer number of numbers may be contained in one segment (256 words). The only limit on the number of elements in a numeric virtual array is the size of the device. Virtual array elements are limited to a length of 2046 characters (bytes). The number of data elements stored in each disk or DECtape segment is a function of the size of each element. For virtual strings, the number of elements is also related to the maximum string length specified in the DIM statement. The size of a virtual string is defaulted to 15 characters, and can be specified as a multiple of three: 3, 6, 9, 12, 15, 18, • . • • . • • 2046. Strings in virtual storage occupy pre-allocated space in the virtual file, and thus differ from strings in core storage, where space is allocated dynamically. A segment containing virtual strings can be considered to be a succession of fields, each of the maximum string length. When a virtual string is assigned a new value, it is stored left-justified in the appropriate field. If the new string value is shorter than the maximum length, the remainder of the field is filled with zeros. When the string is retrieved, its length is computed as the maximum string length minus the number of zero-filled bytes. 8.3 TRANSLATION OF ARRAY SUBSCRIPTS INTO FILE ADDRESSES In order to translate an array subscript into a file address, 8-4 EDUBASIC computer Ca) the relative distance from the specified item to the first item in the array, and then adds (b) the relative distance from the first element of the array to the first item in the file. The first quantity Ca) is computed from the array subscript and the number of elements per block. The second number (b) is a constant for each array in a file, and is computed from the parameters specified in the DIM statement. Since the DIM statement contains the only information used to define the structure of a file, it is possible for the user to specify different accessing arrangements for the same file in one or more programs. For example, the user can reference the same data as either a series of 16-byte strings (A$) or 32-byte strings (B$), with the following statements: l~ 2~ 3~ OPEN 'FILl' AS FILE 1 DIM #l,A$(l,~g) = 16 DIM #1,B$(5g,) = 32 !VIRTUAL ARRAY FILE. !16 CHARACTER STRINGS. !32 CHARACTER STRINGS. The user should keep in mind that in EDUCOMP BASIC, as in most BASICs, array subscripts begin with g, not 1. An array with dimension n, or (n,m) actually contains n+l, or [(n+l)*(m+l)] elements. User programs may define two-dimensional virtual arrays (except for string arrays) as well as singly dimensioned ones. Two-dimensional arrays are stored on disk or DECtape (and in core) linearly, row-by-row. Thus, in the case of an array X(1,2), the array appears logically as: X(~,~) XCg,l) XCg,2) X(l,,) XCl,l) X(l,2) 8-5 while physically it is stored as: X(~,~) lowest address X(l,~) X (2 ,~) X(~,l) X(l,l) X (2,1) highest address If ~ virtual array is to be referenced sequentially, it is usually preferable to reference the rows, rather than the columns, in sequence. Consider the case in which it is necessary to compute the sum of each row and column in a two dimensional virtual array. Program MATI below does this far more efficiently than program MAT2 below: ,. 10 RE!1 PROGRAt·! '!<!A1't' TO CONPUTE SUHS EFFICIENTLY 20' REM 'AR' CONTAINS VIRTUAL ARRAY 30 REM RC Il IS SUN OF ROV I 40 50 60 70 REN C(J) I S SUN OF COLUMN J OPEN nAB". AS FI LE .1 DIN # 1 1 A C 1 1.3 1 51.3 ) DIM R(10)1 CC51.3) 80 FOR R=l TO 10:RCR)=0:NEX1' R 90 FOR C= 1 1'0 50: C ( C) =10: NEXT C J00 FOR J =J 'I'~ S0 J10 FOR I 1 TO 10 120 R(I) RCI) + ACIIJ) J30 CeJ) CeJ) + AeIIJ) 140 NEXT I J S1.3 NEXT J. 160 FOR B=l TO 10: PB! NT R( RH : NEXT R ' 170 FOR C=l TO C:PRIN'I' CCC).;:NEXT C 999 END = = = READY ' .. ".:., "~: ", .-,"'. 8-6 !~PEN VIRTUAL FILE !1!.3 ROWS 1 50 COLUMNS ! I NI 'I' tALI Z E SuriIS !OPERA'I'EJONE COLUMN AT A TH1E ! AND EACH RQT;r IN COLUI-lN !'I'OTAL ACROSS ROW !TOTAL DO~N COLUMN ! NEXT Rm~' INC OLm1N !NEX'I' COLUHN IPRIN'I' ROW TOTALS !PRINT COLUHN TOTALS 10 REB PROGRAM 'HP.T2' HA~ INEFFICIENT 20 REM 'AP' CONTAINS VIRTUAL ARRAY 30 REM RCI) IS SUM OF ROW I 40 REN C( J) IS SillI OF. COLm'iN J 50 OPEN ~'AR". AS FI LE 1 610 D11-1 tfil;.A e 10 .. 50 ) 710 DIN ReID) .. C(50) ,80 FOR R=l TO 10:PCP)=0:NEXT R .90 FOR C=l TO 50:CCC)=0:NEXT C 100 • FOR I = J TO 10 JIQl FOR J = 1 TO 50 1210 RCI) RCI) + ACI .. J) 130 ce~) CCJ) + ACI .. J) .IL10 NEXT J .150 NEXT" I 1610 FOR R=l TO 10:PRINT RCR)~:NEXT R 170 FOR C=l TO C:PRINT CCC);:NEXT C 999 END = = USE OF VIRTUAL CORE LOPEN VIRTUAL FILE !10 ROWS .. 50 COLUMNS ! I NI TIALI Z E SU!-lS ! OPERATE ROi,T BY RO~'! !DO EACH COLUl1N IN ROt} !TOTAL ACROSS ROW ! TOTAL DOFN COLDr1N ! NEXT COLUHN IN ROV ! NEXT ROH !PRINTROWTOTALS !PRINT COLUHN TOTALS READY In virtual arrays it is permissible to have two (or more) arrays sharing the same file. That is, the following DIM statement is perfectly legal: l~~ DIM #1,A(l~~~),B(999) ,C(l~~~) The matrix B begins immediately after the lOOOth element of A and the matrix C begins immediately after B(999). Therefore, the disk layout is as follows: 8-7 A(~) A( 1) ~~ · · · A(999) ~ A(l~~~) B (~) B (1) · ~~ · r~ · B (998) B(999) C (~) C (1) · · · ~r' ~~ C(999) C(l~~~) Figure 8-1 8.4 Virtual Array File Layout ACCESS TO DATA IN VIRTUAL ARRAYS Only a portion of a virtual array is in memory at any given time. This data is transferred directly between the disk and an I/O buffer in the user core area, created when the OPEN statement is executed. segment) long. This buffer is 256 words (one For each virtual array file, EDUBASIC notes (1) the segment of the file in the buffer, and (2) whether or not the data in the buffer has been modified since it was read into core. 8-8 After BASIC translates a virtual. array address into a file address, it checks whether or not the segment containing 'the referenced item is currently in the buffer. If the necessary segment is present the reference proceeds; but if not, another portion of the file is read into the buffer. If the current data in the buffer has been altered, it, is necessary to rewrite this data on the disk prior to reading new data into the buffer. The referencing algorithm, which minimizes the number of disk memory accesses generated when handling virtual arrays, is flowcharted in Figure 8-2. 8-9 Virt:ual Array l:;;efer::;.nc;e ) 'rranslate SUD- I script into File !...._____~....A""<;l"'"q e s s 1 in File .. j No Set: I Modified I ,--_...:1::;.:"x~l(;.;.l;:;.ic~a..:;;t..:;;o~r_ _..1 f"-----II __.__ _.... "'-1-'-r-o-,,-':(-}(-'~'~--\V-j-,t-:J~] ..... Opl'I'.:ILion .. .....-.................. I" i. iJ \ I \', i II "", >~ APPENDIX A LANGUAGE SUMMARY SUMMARY OF VARIABLE TYPES A.I ~ Variable Name Examples Numeric (floating point) single letter optionally followed by a single digit A I X3 Character String any letter name.followed by a $ character M$ R/.$ any numeric variable name followed by one or two dimension elements in parentheses S (4) N2 (a) any character string variable name followed by a one dimension element in parentheses C$ (1) . Numeric Matrix Character String Matrix A.2 E(5,1) va (3,3) SUMMARY OF OPERATORS ~ Arithmetic t *,/ +,- Operator Operates Upon unary minus exponentiation multiplication ,division addition, subtraction numeric variables and constants = Relational equals string or numeric variables less than (= and cons tan ts less than or equal to greater than > >= greater than or equal to () ,# not equal to String + < concatenation string constants and variables EDUCOMP BASIC STATEMENT SUMMARY A.3 The following summary of BASIC statements defines the general format for the statement and gives a brief explanation of its use. CHAIN dev:filnam.ex,line number Terminates execution of user program, loads and executes the specified program 'starting at the line number if included. \ CLOSE n Closes the logical file specified. If no file number is specified, closes all files which are open. DATA data list Used in conjunction with READ to input data into an executing program. DIM variable(n), variable (n,m) Reserves space for lists and tables according to subscripts specified after variable name. END FOR variable Placed at the physical end of the program to terminate program execution. = expressionl TO expression2 STEP expression 3 Sets up a loop to be executed the specified number of times. GOSUB line number Used to transfer control to the first line of a subroutine. GO TO line number Used to unconditionally transfer control to other than the next sequential line in the program. IF expression rel.op. expression THEN line number Used to conditionally transfer control to the specified line of the program. IF expression rel.op. expression THEN statement Used to conditionally execute the statement after the THEN. IF variable THBN .t.t~m~~t ?or the logical 'IF', when the ~ftriable is zero, the statement i:s not executed. INPUT list V:sed to input data from the terminal or papertape reader. :~yboard INPUT #exprsssion, Jist ~nputs INPUT LINE string Inputs a record at a time. Accepts .s::pmmas and quotes, and recognizes -t.be RETURN as the delimiter. from a particular device. INPUT LINE #sxpression, string lnputs a record from a specified ~vice. \. ~nsaves the file. File may ~he form dev:filnam.ex or a KILL file ~-tring variable. -tension. ) [LET] variable ~ be of scalar (Must have an ex- expression ~:sed to assign a value to the specified variable(s). NEXT variable Placed at the end of a FOR loop to return control to the FOR statement. ON expression GOTO list of line numbers The formula is evaluated and control t.ransfers to the first, second, third, ~tc., line number depending on whether the truncated evaluation is 1,2,3, etc. If the magnitude of the index is greater than 2047, an error is generated. Otherwise, if the index is out of range, control passes to the next !It..atemen t • ON expression GOSUB list of line numbers Same as the ON-GOTO statement except t.hat a GOSUB is generated. OPEN file FOR !INPUT l AS FILE #n 'pUTPUTj PRINT list Opens a sequential file for input or output. File may be of the form 4ev:filnam.ex or a scalar string variable. Variables must be DIMen!lional in a separate statement. Used to output data to the terminal. The list can contain expressions or text strings. ?~~~~ t@~t ?~:~ Used to print a message or a string Of characters. j@~p%@§§ion, P~J~~ j:'jM3 (~) li$t Outputs to a particular output device, as specified in an OPEN statement. Used to space to the specified column unless the column is already passed in which case TAB is ignored. ~~~JZ~ Causes the random number generator to calculate different random numbers every time the program is run. nAP -vg%ii#b1.@ l.i$t Used to assign the values listed in a DATA statement to the specified v{f1ri {f1bles • Used to insert explanatory comments into a BASIC program. Used to reset data block pointer so the same data can be used again. Used to return program control to the statement following the last C;OSUB statement. Used at the logical end of the program to terminate execution. \ EDUCOMP BASIC COMMAND SUMMARY A,4 COMMAND EXPLANATION CATALOG Returns the user's file directory. Unless another device is specified following the term CAT or CATALOG, the 'DSK' is the assumed device. COMPILE Allows the user to store a compiled version of his BASIC program. The file is stored with the current name and the extension .BC. Or, a new file name can be indicated and the extension .BC will still be appended. DELETE nl,n2-n3,n4 Removes line numbers nl and n4, as well as lines n2 through n3 inclusive, from the program currently in memory. EDIT line number After EDIT followed by a line number and RETURN is typed, EDUBASIC waits until the search character is typed (but not printed). The specified line is then listed until the first occurrence of the search character. " EXECUTE dev:filnam.ex,line number Runs the specified program. Compiled or .BC programs are tried first. LIST dev:nl,n2-n3 Prints out the current program on the device specified (console assumed). Prints out the specified program line(s) if given. NLIST dev:nl,n2-n3 Same as LIST but without line numbers. LISTNH dev:nl,n2-n3 Lists the lines associated with the specified numbers but does not print a header line. MARGIN line number Changes the maximum line length on all output devices. ) CoMMAND EXPLANATION NEW rilnam Does a SCRatch and sets the current program name to the one specified. OLD dev:filnam.ex line number,step Does a SCRatch and inputs the program from the specified file. Line numbers are added (if specified) to ASCII files not already containing them. OVERLAY dev:filnam.ex,increment Works like OLD but does not scratch. PUNCH nl,n2-n3 Punches the current program on the fastest available papertape punch. NPUNCH nl,n2-n3 Same as PUNCH but no line numbers are punched. RENAME filnam Changes the current program name to the one specified. REPLACE dev:filnam.ex nl,n2-n3 Replaces the specified file with the current program. Parts of the program may be replaced by specifying particular line numbers. NREPLACE dev:filnam.ex nl,n2-n3 Same as REPLACE but line numbers are not saved. RESEQUENCE line number, increment Renumber the lines in a program and changes appropriate GOTO, IF-THEN, etc. If line number is not specified, starts at 100 with increments of 10. RUN Executes the program in memory. RONNE Executes the program in memory but does not print a header line. SAVE dev:filnam.ex nl,n2-n3 Outputs the program in memory as the specified file. NSAVE dev:filnam.ex nl,n2-n3 Like SAVE but does not save line numbers. SCRatch Erases the entire storage area. ) COMMAND EXPLANATION SEARCH nl·n2/stringA/ Lists all lines in the range nl to n2 that contain string A anywhere in the line. SEARCH nl-n2/stringA/stringB/LIST String B replaces all occurrences of string A and these lines are listed if LIST is specified. TAPE line number,increment Like OVERLAY, but the file comes from the fastest available papertape reader. SPECIAL CONTROL CHJ\RACTER SUMMARY CONTROL CHARACTER EXPLANATION CTRL/C Causes the system to return to the os/a monitor. CTRL/P Returns BASIC to the READY mode. CTRL/O Used as a switch to suppress/enable output of a program on the user terminal. Echoes as to. CTRL/U Deletes the current typed line, echoes as tu and performs a carriage return/ line feed. CTRL/Z Used as an end-of-file character. LINE FEED Key Used to list the current line. RETURN Key Enters a typed line to the system, results in a carriage return/line feed operation at the user terminal. RUBOUT Key Deletes the last character typed on that physical line. Erased characters are shown on the teleprinter between back slashes. TAB or CTRL/I Performs a tabulation to the next of nine tab stops (eight spaces apart) which form the terminal printing line. ) SUMMARY OF FUNCTIONS A.6 Under the Function column, the function is shown as: Y=function where the character '$' is appended to Y if the value returned is a character string. Function Explanation Y=ABS (X) Y=ATN (X) Y=COS (X) Y=EXP(X) Y=INT(X) returns the absolute value of X. returns the arctangent of X in radians. returns the cosine of X in radians. returns the value of etx, where e=2.7l828. returns the greatest integer which is' less than or equal to X. returns the natural logarithm of X, log eX. has a constant value of 3.141593. returns a random number between ~ and 1. returns the sign function of X, a value of 1 preceded by the sign of X. returns the sine of X in radians. returns the square root of X. returns the tangent of X in radians. Y=LOG(X) Y=PI Y=RND(X) Y=SGN (X) Y=SIN(X) Y=SQR(X) Y=TAN (X) returns the current position of the print head for I/O channel X, ~ is the user's Teletype. moves print head to position X in the current print record, or is disregarded if the current position is beyond X. (The first position is counted as ~.) Y=POS (X) Y$=TAB(X) Y=ASCII (A$) returns the ASCII value of the first character in the string A$ • returns a character string having the ASCII value of X. Only one character .. '. is generated. returns a substring of the string A$ starting with the Nl and being N2 characters long (the characters betw'3en and including the Nl to Nl+N2-1 characters). returns the number of characters in the string A$, incluQing trailing blanks. . Y$=CHR$ (X) Y$=MID(A$,Nl,N2) Y=LEN(A$) ) ./ ' APPENDIX B ERROR MESSAGES The error messages appearing onthe following pages are designed to specifically to help the use pinpoint the 'bug' in his program quickly. An arrow' (+) is used in many statement to point to the offending syntax and in most error messages the line number of the statement in error is given. COMPILER ERRORS B.1 MESSAGE CAN'T 'IF' VIRTUAL CORE STRING EXAMPLE EXPLANATION 1flfl OPEN "CORE" AS FILE 1 Us~r cannot have a virtual core string in an 'IF' statement. . 11' DIM #1,A$=3', 12' IF A$="ONE"THEN PRINT "ONE" 13' END RUNH 12f,Y IF A$="ONE"THEN PRINT"ONE" t CHARACTERS AFTER STATEMENT END 1', INPUT LINE A$,B$ 11' END RUNH Statement has unrecognized characters at the end of it. 10' INPUT LINE A$,B$ t COMPILER ERROR 1'11 DIM A$(3,2) 11' END RUNH l~fl 'ENO' NOT LAST AT L1NE 11, O!M A$(3t~) + U8f1 PIUNT "'A.... ll~ The user has used a legal statement in an illeqal manner .. I5:ND 1.216 GO TO 198_ RUNa 'The 1-a~st ~tat.~~n.t l'ftu.'st b~ ~n 'END' :slt-abe\'l\'enlt" (/ MESSAGE EXAMPLE 2~~ 3~~ EXTRA OPERATOR EXPLANATION IF A==B THEN END 3~~ The statement contains an extra operator. RUNH 2~~ IF A==B THEN 3~~ t 1~~ 11~ EXTRA • (. Line has one more left parenthesis than right parenthesis. Y=( (A+B)/5 END RUNH 1~~ Y= ( (A+B) /5 t 1~~ EXTRA ')' 11~ Line has one more right parenthesis than left parenthesis. Y=(A+B»/5 END RUNH 1~~ Y=(A+B»/5 t 1~~ FILE TOO LARGE OPEN "DTA1:HALT" AS FILE 1 A file is dimensioned too large for any device. 11~ DIM #l,Q(l~~~~~~) 12~ END RUNH 11~ DIM #l,Q(l~~~~~~) t 'FOR' WITHOUT 'NEXT' AT LINE 1~~ 1~~ 12~ FOR I=l TO END 5~ A variable used as the index in a 'FOR" statement does not appear in a corresponding 'NEXT' statement. RUNH ,/ MESSAGE EXPLANATION EXAMPLE ILLEGAL ASSIGNMENT ILLEGAL CONSTANT The assignment made is not acceptable to BASIC l~~ Y=88888888888 ll~ END A number inside the program cannot be longer than 10 digits. A number that is input can be any length. RUNH l~~ Y=88888888888 t ) ILLEGAL INTEGER l~~ ll~ DIM A$="3~" END A string's length must be an integer number. RUNH l~~ DIM A$="3~" t ILLEGAL STRING VARIABLE l~~ ll~ A string variable must be a single letter followed by a ,$,• Al$="PDP" END RUNH l~~ Al$="PDP" t ILLEGAL SUBSCRIPTING l~~ ll~ A subscripted variable has been dimensioned or used incorrectly. PRINT A(S END RUNH l~~ PRINT A(S t :rLLEGAL SYNTAX l~~ ll~ A string variable has been A$=7 END used where a numeric variable should have been used. RUNH \ MESSAGE EXPLANATION EXAMPLE ILLEGAL USE OF FUNCTION l~~ ll~ Y = LOG l~ (X) END RUNH l~~ Y = LOG l~ A function must be followed by an open parenthesis, an argument and then a closed parenthesis. (X) t ( ILLEGAL VARIABLE l~~ ll~ variable must be one letter or one letter followed by a number. PRINT FI END A RUNH l~~ PRINT FI t INCONSISTENT SUBSCRIPTING l~~ ll~ 2~~~ DIM A$(l~) PRINT A$(l,l) END RUNH ll~ string variable has been dimensioned as a one-dimensional variable and utilized as a two-dimensional variable. A PRINT A$(l,l) t , MISPLACED OR l~~ ll~ comma or semicolon doesn't belong where it was placed. A=5 END A RUNH l~~ A=5, t MISSING '=' l~~ ll~ Y-5 END Statement requires an equal sign. RUNH l~~ Y-5 t MISSING , The syntax requires a comma \n the designated position. EXAMPLE MESSAGE MISSING OPERATOR EXPLANATION 111 PRINT "TESTl""TEST2" 211 END RUNH The statement is missing an arithmetic or relational operator, or a punctuation mark. I' PRINT I TEST1"ITEST2" t MISSING I (I 1" Subscripted variable has been used as a non-subscripted variable. DIM A$(4)=211 II, A$=5 12' END RUNH II' A$=5 t MISSING QUOTE 1', PRINT II ABC II' END RUNH A string variable assignment statement must have the assigned value surrounded by quotes. l'ft PRINT "ABC MISSING VARIABLE t I, PRINT TAB();X 211 END RUNH The statement is missing a numeric variable or constant. 111 PRINT TAB();X t MIXED MODE EXPRESSION A numeric variable was used where a string variable should be used. 1', Y="ABCD" II' END RUNH 1" Y="ABCD" t \ MESSAGE EXPLANATION EXAMPLE 'NEXT' WITHOUT 'FOR' AT LINE 11. NO 'END' STATEMENT 1" A variable used as the index in a 'NEXT' statement does not appear in a corresponding 'FOR' statement • l~~ PRINT "ABC" Program must have an 'END' statement. DEF FNA(X)=X+2 END BASIC does not understand the statement. RUNH l~~ ll~ NON-BASIC STATEMENT . FOR I=l TO 5. 11~ NEXT A 12~ END .RUNH RUNH l~~ DEF FNA(X}=Xt2 t Program. is too long for BASIC to compile. PROGRAM TOO LONG 'READ' WITHOUT 'DATA' ON LINE l~~ READ A END The program contains one or more 'READ' statements and no ,'DATA' statements. DIM A(2~~~) 11f1 END RUNH Not enough space in core for all of the subscrip.ted variables in the program. l~~ 2~~ RUNH TOO MANY ARRAYS AT LINE 11~ l~~ TOO MANY LITERALS The program has too many literals for BASIC to handle. TOO MUCH DATA AT LINE 31 Program has too much data in its DATA statements for BASIC to handle. \ MESSAGE UNDEFINED LINE NUMBER AT LINE l~~ EXAMPLE l~~ ll~ GO TO END EXPLANATION l5~ a non-existent line (GOTO, GOSUB, ON-GOTO, ON-GOSUB, IF-THEN) • RUNH VARIABLE DIMENSIONED TWICE l~~ ll~ l2~ DIM A$=l5 DIM A$=2~ END RUNH ll~ Any statement which references DIM A$=2~ t A variable must appear in only one dimension statement. RUNTIME ERRORS B.2 EXAMPLE MESSAGE ARRAY OF WRONG SIZE IN LINE l~~ 9~ OPEN "A" AS FILE 1 DIM #l,A(l~~~) 2~~~ END RUNH A virtual file cannot be dimensioned larger than at the time it was created unless the original is deleted. l~ Only a BASIC program can be chained. l~~ BAD FILE FOR CHAIN IN LINE l~ EXPLANATION CHAIN "CDR3.DA" 2Y1 END RUNH BAD INPUT IN LINE lY1Y1 INPUT A l~~ llY1 END RUNH Numeric variables may have only numbers as input. ? l~P CAN'T OPEN OUTPUT FILE IN LINE l~~ lY1~ lll~ OPEN "CDR:" FOR OUTPUT AS FILE 1 END RUNH CHANNEL NOT OPEN FOR INPUT IN LINE lY1~ lf1~ INPUT #l,A llY1 END RUNH CHANNEL NOT OPEN FOR OUTPUT IN LINE lY1~ lY1Y1 PRINT #l,A llY1 END RUNH A sequential access file cannot be opened because the device specified is full or there is a mistake in the 'OPEN' statement. No file or device has been opened under the specified channel number. No file or device has been opened under the specified channel number. MESSAGE CHANNEL OUT OF RANGE IN LINE l~~ EXAMPLE l~~ ll~ OPEN "EDU" AS FILE 5 END EXPLANATION Files can only be opened under numbers 1-4. RUNH DEVICE ERROR DEVICE FULL IN LINE 2~ A device that the system has been configured for has been used in an illegal manner. l~ 2~ 2~~ OPEN "AFILE" AS FILE 1 DIM #1,A(5~~~~) END Device specified doesn't have enough contiguous blocks to contain file. OPEN "AAA:RISK" AS FILE 1 END Any device for which the system is not configured cannot be accessed. RUNH DEVICE NOT AVAILABLE IN LINE l~~ l~~ ll~ RUNH DIVISION BY ZERO IN LINE 7~ 711 PRINT T/Y 21111~ END RUNH END OF FILE IN LINE 2~ 5 DIM A$=1211 111 OPEN "CDR" FOR INPUT AS FILE 1 211 INPUT LINE #1, A$ 30 GO TO 2(1 ll1~ END RUNH input device or file has no more elements remaining. ERROR CLOSING FILE IN LINE l~~ l~~ A sequential access file cannot be closed because the specified device is full. Division by zero is an undefined operation. OPEN "K" FOR OUTPUT AS FILE 1 llfi1 PRINT #l,A 115 GOTO ll~ l2~ END RUNH An EXPLANATION EXAMPLE MESSAGE ERROR READING FILE 1" OPEN"READ"FOR INPUT AS FILE 1 11g END RUNH Input from device or file contains an unrecognizable error. FILE ALREADY OPEN IN LINE 1~' 1~11 A file or device which the program attempts to open has previously been opened. OPEN"LOAN"FOR OUTPUT AS FILE 1 1115 A=l~ 11~ PRINT #l,A 12~ GOTO.1~~ 4ftlftl END FILE NOT FOUND IN LINE 1" RUNH Iftlftl CHAIN ftINFORTW,2" 11.f.J END RUNH FUNCTION ARG TOO BIG IN LINE 1~~ 1f111 Y=2t1.f.J.f.J~~ File specified does not 'exist. 11.f.J END RUNH BASIC cannot manipulate the function with the specified arguments. 2f1f1 CHAIN "PLOT",12 2f1f1r1 END There is no such line in the .program chained. MID ERROR IN LINE 2~ 111 A$="ABCD" 2~ B$=MID(A$,Sg,S) 3g END RUNH A MID function must have a positive integer for its length specification, it must have at least one character to 'MID', and it must not 'MID' past the dimension of the string. NEGATIVE OR ZERO LOG IN LINE 1~' 1~11 The LOG function requires a positive argument. LINE NOT FOUND IN LINE 2~~ RUNH Y=LOG(g) llg END RUNH MESSAGE EXAMPLE NO CLOSING QUOTE IN LINE 1~' 1'~ INPUT A$ 11' END RUNH ? "ABC CR. NOT A BINARY FILE IN LINE 1~' 1'~ DIM #4,A$(l')=2' ll~ 1" . l~~ ll~ l2~ A string variable must have a closing quote if it has .an opening quote. END A virtual file must be opened before it is dimensioned. READ A, B, C, D DATA 4,7,2 END A READ statement has no more data available to read. RUNH OUT OF DATA IN LINE EXPLANATION RUNH OUT OF STORAGE IN LINE 5' RETURN WITHOUT GOSUB IN LINE Program has run out of storage performing an operation. 1" l'~ RETURN ll~ END A RETURN statement must only be accessed after a GOSUB command has previously been executed. Y=SQR (-'9) END The SQR function requires non-negative argument. RUNH SQR OF NEGATIVE ARG IN LINE 1', l~~ ll~ RUNH STEP OF , IN LINE 1" l~~ FOR 1=1 TO l ' STEP X NEXT I 4"~ END RUNH 2~~ The step of a FOR-NEXT loop must be a non-zero number. MESSAGE EXPLANATION EXAMPLE STOP AT LINE 15' 15' STOP 2" END R~H 1~~ STRING OVERFLOW IN LINE 11' 11~ 12~ DIM A$=4 A$="1234s6" END RUNH SUBSCRIPT OUT OF BOUNDS IN LINE ll~ 1~~ ll~ 12~ DIM A(s) PRINT A(6) END Execution has been halted by BASIC at the line indicated. A string must not be set equal to a length greater than its demension statement. A subscript of a subscripted variable has exceeded its DIMension. RUNH TAN OF PI/2 IN LINE 45 45 PRINT TAN(PI/2) END RUNH 2~~fJ An error has occurred which 'BASIC does not know how to handle. UNDEFINED ERROR IN LINE 25 ZERO TO ZERO POWER IN LINE The tangent of PI/2 does not exist. l~ l~ 2~~fJ PRINT xtx END Zero to the zero power does not exist. RUNH , } COMMAND ERRORS B.3 EXAMPLE MESSAGE EXPLANATION DEVICE ERROR SAV PTR: If you try to use a device the system is configured for in an illegal manner, this error statement will result. DEVICE NOT AVAILABLE SAV PTT: System is not configured for the specified device. ERROR DELETING FILE n UNSAVE COMP File n not found. ERROR READING FILE OLD PAYROL An error has occurred in calling a previously saved program into core. filnam.ex ALREADY SAVED SAVE The specified file already exists on the specified device. o filnam.ex NOT FOUND OLD COMP File specified is not found on specified device. ILLEGAL FILE NAME OLD A-5 File name must be less than six characters, consisting of alphanumeric characters, and starting with a letter. LINE NOT FOUND EDIT ll~ When using the EDIT command, the line specified does not exist. MESSAGE LINE NUMBERS MISSrNG ON filnam.ex EXAMPLE EXPLANATION File called in is missing some or all line numbers. OLD A.DA Line greater than 124 characters. LINE TOO LONG NOT A FILE DEVICE UNSAVE PTP:AA The specified device can't be used to save files. NUMBER OUT OF RANGE l~~~~END Line numbers must be in the range 1-4094. PROGRAM TOO LONG TO RESEQUENCE RESEQUENCE File in core contains too many characters for BASIC to resequence. SEQUENCE NUMBER OVERFLOW l~~PRINT ll~ PRINT l2~ PRINT l3~ "A" "B" "c" PRINT "D" l4~ END RESEQUENCE l~~~,l~~~ Specified command needs more arguments than were given. When trying to resequence, a line number became greater than 4094. TEXT BUFFER IS FULL OLD LCARDS.DA l~,l TOO FEW ARGS SEARCH TOO MANY LINES OLD CARD.DA l~,l~ User tried to type in or call in a file with more lines than is acceptable to BASIC. WHAT?? MISTNH BASIC does not recognize the command. 1~~-2~~/A File is too big to fit into core or too large to be able to use the SEARCH command. Specified command needs more arguments than were given. INDEX ABS function, 3-19, A-8 Arithmetic operators, 2-7, A-l Array storage, 8-4 Array variables, 3-15 character string, 4-3 default values, 3-17 virtual core, 5-10, 8-1 zero elements, 3-17, 8-5 see also matrices ASCII DATA statement, 4-8, 6-2 formatted I/O, 5-3 table, 4-6 . ASCII function, 4-10 Assignment, see LET BASIC conventions, 1-4 history, 1-1 language, 1-1 start-up, 1-4 Brackets, 1-5 Buffer, 8-8 Capital letters, 1-5 CATALOG command, 7-19, A-5 CHAIN statement, 5-14, A-2 Channel numbers, see Internal file designators Characters, 2-4 Character strings, 4-1 constants, 4-2 functions, 4-9, A-8 output by PRINT, 4-1, 4-8 relational operators, 4-5, A-l size, 4-4 string input, 4-7 string output, 4-9 subscripted variables, 4-3 variables, 4-;2 virtual core arrays, 5-11, 8-4 CHR$ function, 4-10 CLOSE statement, 5-9, 5-13, A-2 Colon, 2-4 Commands, 1-3, 1-5 summary, A-5, see Chapter 7 for specific commands Commas in DATA, 4-7 in INPUT LINE, 6-3 in PRINT, 6-4 in PRINT-USING, 6-8 Comments, 3-27 DATA statement, 6-1 Common statement similarity to, 8-2 Compilation, 1-3 Compiled files, 7-2, 7-13, 7-17 COMPILE command, 7-17, A-5 Concatenation, 4~10 Conditional branch, 3-6 Conditions, 3-6 see also relational expressions Constants character string, 4-2 numeric, 2-5 Control characters summary, A-7 CONTROL key, 1-6 Control variable, 3-12, 3-15 Conventions, manual, 1-4 COS function, 3-19, A~8 Creating a program, 7-1 CRTL key, 1-6 CTRL/C, 1-7, 7-24, A-7 CTRL/G, 7-28 CTRL/I, 7-25, A-7 CTRL/L, 7-7 CTRL/O, 7-25, A-7 CTRL/P, 1-7, 7-24, A-7 CTRL/U, 1-6, 7-7, 7-24, A-7 CTRL/Z, 7-25, A-7 Data files, see files, data Data pool, 3-3, 4-8, 6-1 DATA statement, 3-3, 4-7, 6-1, A-2 character strings in, 4-7, 6-1 comments, 6-1 data pool storage, 3-3, 4-8, 6-1 placement in line, 4-8, 6-1 simplest form, 3-4 DELETE command, 7-9, A-5 Device designator, see Internal file designator Devices, OS/8, 5-1 x-l DIM statement, 3-17, 4-3, A-2 placement on line, 3-18 placement in program, 3-18 virtual files, 5-10 . Disk files, 8-1, 8-4, 8-8 Dollar sign ($), 2-4 EDIT command, 7-6 Editing programs, 7-6 E format numbers, 2-5 END statement, 3-26, A-2 Error messages, Appendix B Example BASIC program, 2-2 Exclamation mark (1), 2-4, 3-27, 6-6 EXECUTE command, 7-13, 7-18, A-5 Execution, 1-4, 2-3 EXP function, 3-19 Exponential format output, 6-8 Exponentiation, 2-7, 3-8 Expressions, 2-4, 2-7, 3-6 arithmetic, 2-4, 2-7 relational, 2-8,3-6 Extensions, file, 5-2, 7-2 Filename format, 5-1 Filename specification, complete, 5-1 Files, data, 9-1 calling into memory, 7-4 formatted data, 5-3 random access, 5-10 see also virtual array files Files, DECtape, 8-1, 8-4 Files, disk, 8-1, 8-4, 8-8 File-structured devices, 5-4 Formatted ASCII I/O, 5-3 Formulas, see expressions FOR statement, 3-12, A-2 nesting loops, 3-14 placement on line, 3-15 Functions mathematical, 3-8, 3-18 print, 6-9 string, 4-9 summary, A-8 GOSUB statement, 3-24, A-2 GOTO statement, 3-5, A-2 IF-THEN statement, 2-8, 3-6, A-2 logical, 3-9 placement online, 3-9 Implicit dimensions numeric, 3-17 string, 4-3 Input character strings, 4-7 see also READ, INPUT, and INPUT LINE INPUT LINE statement, 4-8, 6-3, A-3 INPUT statement, 3-4, 5-4, 5-9, 6-2, A-3 character string input, 4-8 from data files, 5-4 from non-terminal devices, 5-4, 5-9, 6-2 simplest form, 3-4, 6-3 Internal file designators, 5-1, 5-4, 5-6 user terminal, 5-6 virtual array, 5-7, 5-11 Intrinsic functions, see functioning INT function, 3-19, A-8 I/O, basic operations, 3-2 complete discussion, Chapters 5 and 6 see also individual entries Kemeny, John, 1-1 KILL statement, 5-14, A-3 Kurtz, Thomas, 1-1 LENGTH command, 7-19 LEN function, 4-10, A-8 LET statement, 3-1, A-3 multiple variables, 3-2 omitting LET, 3-3 placement on line, 3-3 Line, 1-5, 2-1, 7-23 multiple statements on single, 2-3 single statement on multiple, 2-3 LINE FEED key, 1-7, A-7 Line terminators, 4-8 LIST command, 7-10, A-5 LIS.TNH command, 7-10, A-5 LOG function, 3-19 Logical IF-THEN statement, 3-9 Loops, 3-7, 3-10 characteristic parts, 3-11 nested, 3-14 x-2 Lower case type, 1-5 Mapping, 8-1 MARGIN command, A-8 Mathematical functions, 3-18, A-8 table, 3-19, A-8 Mathematical operators, 2-7, A-l Matrices, 3-15 implicit dimensions, 3-17 virtual core, 5-10, 8-5 Memory, conserving, see programs Message output by PRINT, 3-3 MID function, 4-10, A-8 Minus sign (-), 2-7, 3-8 Multiple lines per statement, 2-3 Multiple statements per line, 2-3 Nesting loops, 3-14 subroutines, 3-25 NEXT statement, 3-12, A-3 placement on line, 3-15 NLIST command, 7-10, A-5 NLISTNH command, 7-11 None, assumed filename, 7-2 NPUNCH command, 7-21, A-6 NREPLACE command, 7-17, A-6 NSAVE command, 7-16, A-6 Null string, 4-3, 4-5 Number format, output by PRINT statement, 3-3 Numbers, 2-5 E format, 2-6 Number sign (#), 2-4, 6-7 OLD command, 7-3, A-6 ON-GOSUB statement, 3-28, A-3 ON-GOTO statement, 3-28, A-3 OPEN statement, 5-2, 8-8, A-3 FOR INPUT, 5-3, 5-9 FOR OUTPUT, 5-3, 5-8 user terminal, 5-6 virtual array file, 5-3, 5-12 Operators mathematical, 2-4, 2-7 relational, 2-8, 3-8 summary, 5-1, 5-4, A-l OS/8, 1-4, 1-7, 4-5, 5-2, 7-1, 7-20, 7-22 Output character strings, 4-9 see also PRINT OVERLAY command, 7-5, A-6 Parentheses, 2-4, 2-7, 3-8 PI, 3-19 Plus sign (+), 4-10 Pound sign (#), 2-4, 2-9, 6-7 Precedence rules complete, 3-8 mathematical, 2-7, 2-8 PRINT functions, 6-9 PRINT statement, 3-3, 6-4, A-3 character string format, 4-1, 4-9 comma, 6-4 message output, 3-3 number format, 6-5 output rules, 6-5 performing calculations, 3-3 semicolon, 6-5 simplest form, 3-3, 6-4 to data files, 5-4, 5-8, 6-6 to non-terminal devices, 5-4, 5-8, 6-6 without arguments, 3-3, 6-4 PRINT-USING statement, 6-6 exclamation point, 6-6 exponential format, 6-8 numeric field, 6-7 punctuation, 6-8 string field, 6-7 Print zones, 6-4 Priorities, see precedence rules Programs, 1-5 creating, 7-1 conserving memory space, 2-4, 3-18 line, 1-5 running, 1-3, 7-12 PUNCH command, 7-21, A-6 Question mark (?), printed by INPUT, 3-4, 6-3 Quote ("), 2-4, 4-7, 6~l, 6-3 Random access files, see virtual array files RANDOM statement, 3-22, A-4 RAN'DOMIZE statement, 3-22, A-4 x-3 READ s·tatement, 3-3, 4-7, 6-1, A-4 placement in line, 4-8, 6-1 simplest form, 3-3 string input, 4-7 Relational expressions, 2-8, 3-6, 4-5, A-l operators, 2-8, 3-8, A-l operators with character strings, 4-5, A-l REMARK statement, 3-27, A~4 RENAME command, 7-17, A-6 REPLACE command, 7-17, A-6 RESEQUENCE command, 7-8, A-6 RETURN key, 1-6, 4-9, 6-4, 7-22 RETURN statement, 6-2, A-4 RND function, 3-19, 3-21, A-8 RUBOUT key, 1-6, 7-6, 7-23· RUN command, 7-13, A-6 RUNNH command, 7-13, A-6 Running BASIC, 1-3 Sample BASIC program, 2-2 SAVE command, 7-14 Scientific notation, 2-6 SCRATCH command, A-6 SEARCH command, 7-11, A-7 Semicolon (i) in PRINT, 6-4 in PRINT-USING, 6-8 SGN function, 3-19, A-8 SIN function, 3-19, A-8 Single statement on multiple lines, 2-3 Space, conservation of, see· programs Spaces, 2-4, 4-7, 4-9 Square brackets ([]), 1-5 Statements elementary, 3-1 multiple on single line, 2-3 single on multiple lines, 2-3 summary, A-2 STEP expression, 3-12, A-2 STOP statement, 3-26, A-4 String, see character string Subroutines, 3-23 GOSUB, 3-25 nesting, 3-25 ON-GOSUB, 3-28 Subscripts, 3-15 character string variables, 4-3 default values, 3-17, 4-3 zero elements, 3-17, 4-3 Syntax, 1-4, 2-1 Tabs, 7-25 TAN function, 3-19, A-8 TAPE command, 7-20, A-7 Tapes, paper punching, 7-20 reading, 7-20 Terminal input, see INPUT and INPUT LINE Terminals, 1-5, 5-1, 5-6 Unary minus, 2-7, 3-8 Unconditional branch, 3-5 Up-arrow (t), 6-8, 8-1 Variables character string, 4-2, A-l initial value, 2~7 numeric, 2-5, A-l subscripted, 3-15, 3-17, 4-3 subscripted and unsubscripted in same program, .3.-16, 4-2 summary, A-l Virtual array files, 5-10 example, 5-12 opening, 5-12 see also Chapter 8 Virtual data storage, see virtual array files Zero, assumed value, 2~6, 3-18 Zeroth element numeric, 3-15, 3-17 string, 4-3 x-4
Home
Privacy and Data
Site structure and layout ©2025 Majenko Technologies