BASIC is an acronym for Beginner's Algorithmic Symbolic Instruction Code. Clearly the acronym came first! It was invented by J. Kemeny and T. Kurtz at Dartmouth University as an instructional tool when computer time-sharing was new. Students worked at terminals, perhaps connected with a central computer by acoustic modems. This was much more convenient than batch processing, since students received prompt feedback. It was adopted as a computer control language with the Apple II, where it was called Applesoft, and then for the IBM PC, where it was called BASIC. This began its wide use as a programming language, especially for personal computers. There is even Visual BASIC for Windows programming.
BASIC resembles FORTRAN superficially, but is very different in structure. It collects variables and their values in a linked list, and uses a stack for arithmetic computation. A program is a series of lines numbered in sequence, from memory or from the keyboard, that are interpreted one by one. Of course, BASIC can be compiled and run like any other high-level language, but its ability to interpret instructions entered at a terminal is what made it so useful for small computers. The BASIC interpreter executes a small set of keywords and evaluates expressions. Input comes from the program, or from the keyboard, and output goes to the screen. The BASICs that are used with personal computers are all extended to allow them to handle the necessary duties, but all are compatible with basic BASIC.
The BASIC furnished with the first IBM PC's were BASIC.COM AND BASICA.COM. These small programs were dependent on the edition of DOS, and will not run on Windows 98. QBASIC.EXE and QBASIC.HLP will run, however. These files should be copied into a directory such as C:\basic. Run QBASIC by typing its name at the prompt and pressing Enter. It uses a full-screen character interface, and puts you in the program editor on entry. Press Alt to activate the main menu, then press a highlighted letter to choose that command. File offers the options: N(ew), O(pen), S(ave), (save)A(s), P(rint) and e(X)it. Make a new file named "hello.bas". The current file is shown at the top of the screen. You can start typing at once, and save the file later with its name if you want. There is a lot of Help available.
Type 10 PRINT "Hello, world!" on the first line, and 20 END on the second line (this can be omitted, but it is orderly to express it). Now type Alt R to run the program, and you will see the output on the output screen. There will be instructions on how to get back to the editor. Now we can play with BASIC!
Every line begins with a sequence number that is also a label. This can be at most five digits long. If you use multiples of 10, then there is room for nine afterthought statements to be inserted without having to redo the numbering. Blanks have no significance in BASIC, but they certainly help to make the lines readable. Blanks are not used as token separators in lines, which makes BASIC harder to write, but easier for students to use. A line may be up to 255 characters in length.
BASIC uses upper case only, but lower case may appear in character constants. Nothing special ends a line, but a colon must separate different instructions on the same line (some BASICs use /). A comment line begins with REM instead of a sequence number. Anything after a ' on a line is considered to be a comment. BASIC has many reserved words, since they cannot be redefined.
The arrow keys on the Apple keyboard move the cursor. Every character crossed to the left is erased, every character crossed to the right is retyped. There is no destructive backspace.
A variable in basic BASIC is a single letter or a single letter followed by a number. In QBASIC, a variable can have up to 40 characters. Variables are typed intrinsically by their final character. No special character, or a !, give a single-precision float (6 significant digits). A # means a double-precision float (16 significant digits). A % identifies a 16-bit integer. Strings are stored in variables ending with $. String constants are delimited by "".
Arrays begin at index 0. They are dimensioned before use by a statment like DIM A(9), which gives single-precision float array A 10 elements, from A(0) to A(9). If the DIM is not used, this is the default. DIM B%(20) is an integer array with 21 elements. Arrays can have up to 255 dimensions. Some BASICs use  to delimit array indices, but not QBASIC or Applesoft. QBASIC defines MAT operations to work with matrices, but FOR ... NEXT loops can be used instead.
The arithmetic operators are, in order of precedence: ^ (exponentiation), unary -, * and /, \, MOD, + and -. / is floating-point division, while \ is integer division (operands rounded, quotient truncated). MOD returns the remainder in integral division. The relational operators are: =, <> or ><, <, >, <= or =<, >= or =>. Arithmetic operators are performed before relational operators. Logical operators are NOT, AND, OR, XOR, IMP (implication), EQV (equivalence). The + concatenates strings (some BASICs use , or &).
An assignment statement takes the form
Output to the screen uses a PRINT statement. In QBASIC, PRINT can be abbreviated to "?". PRINT by itself gives a blank line. PRINT with a comma-separated list puts its output at tab stops, 5 across the screen. A ; inserts no space at all. Input from the keyboard is obtained by INPUT, followed by a variable list. INPUT issues the prompt ?, then waits for input. Usually, something like PRINT "A"; : INPUT A, would be used, issuing the prompt A? and waiting for input.
Another way to get input is to use a DATA statment, and then READ values from it. If we have DATA 1.0,2.0,3.0, then READ A will set A = 1.0. The next READ A will set A = 2.0, and the third A = 3.0. The fourth will give OUT OF DATA and halt the program. RESTORE puts the data back to the start.
In QBASIC, PRINT output can be controlled with the PRINT USING A$;
. A$ is a string variable or constant containing formatting information. A digit is represented by #, decimal points are inserted, a leading + prints the sign, and a trailing ^^^^ uses exponential notation. A leading $$ prints a dollar sign. For example, PRINT USING "+###.# ";A;B;C would give -30.3 +501.4 +3.6, if these were the values of the variables A,B,C. The format string is reused if necessary, leading 0's are replaced by spaces, and values are rounded.
Program control in BASIC is very basic. The only structure is the FOR loop, of the form FOR I% = 0 TO 9 STEP 1 ... NEXT I%. The ... represents a block of statements, delimited by the FOR ... NEXT. An unconditional jump is GOTO n, where n is a sequence number. A conditional jump is IF (logical) THEN n, which evaluates the expression (logical), and if it is true, goes to statement number n. If it is false, execution goes to the next statement. All other loops except the FOR loop must be constructed with these simple tools. Therefore, flow charts are very useful for BASIC programming.
A subroutine is a series of lines ending with a RETURN statement. It is called by the statement GOSUB n, where n is the label of the first statement in the subroutine. There is no passing of arguments, but all variables in a BASIC program are global. The programmer should take care to keep subroutines separate, and comment them for clarity.
BASIC defines the intrinsic trigonometric functions SIN(X), COS(X), TAN(X), where X is in radians, and returns radians from ATAN(X). EXP(X) and LOG(X) give the exponential and the natural logarithm. SQR(X) returns the square root (not the square!). ABS(X) returns the absolute value, and SGN(X) returns +1, -1 or 0. INT(X) returns the largest integer not greater than X, or floor(X). RND(X) returns a random number between 0 and 1. The (X) is not required, but reseeds the generator when X is negative. RANDOMIZE n also reseeds the generator in QBASIC. If X = 0, the last random number is repeated. TAB(X) tabs X places when used in a PRINT statement.
User-defined functions are like FORTRAN statement functions. DEF introduces the definition. The function name is FN followed by a letter, and there is a formal parameter list. For example, DEF FNA(X) = X*X defines a function that squares a variable, used as Y = FNA(Z), for example. In QBASIC, the function name may have any variable name, which types the function. This is a very useful feature, but the definition must fit on one line (but a line can be 255 characters long).
The Apple IIe came back to life after more than 10 years of total rest, so it was possible to check Applesoft. I managed to boot DOS, but I forgot that CTRL and RESET have to be pressed at the same time to reset the IIe. Commands are followed by , D2 or , D1 to select one disk or the other. The Apple II comes up in Applesoft, with the prompt ]. If you type a command without a label, it is executed immediately. If you begin with a label, the line will be stored in the program buffer. Execute NEW to clear out the buffer. Normally, the CAPS LOCK is depressed. Although the Apple II keyboard will give lower case, it is only acceptable in string constants. BASIC is case sensitive.
Enter the program 10 PRINT "Hello, world!" and 20 END on two lines. LIST will show what is in the program buffer at any time. RUN executes the program, and you will see Hello, world! on the screen. Save the program in a disk file with SAVE HI (or SAVE HI, D2 if the disk you can write to is in Drive 2). Don't use HELLO, since that is the name of a program on the System Diskette used to greet the user. Use CATALOG to check that the program was saved. You can always LOAD a program from disk so that it can be edited (use LIST to see it). A program can be run from disk. Try RUN HI, D2 (the D2 is probably not necessary, since the Apple remembers the last disk drive used).
I notice that I also have FORTRAN and Pascal for the Apple II, as well as IEEE 488, serial, and parallel ports. It is easy to interface peripherals with the Apple II, though timing considerations are critical. It is also easy to use machine language subroutines with Applesoft. There is room for 240 bytes at addresses $300-$3EF, and HIMEM: can be used to protect routines at the upper end of RAM. All of this offers interesting programming opportunities.
J. Sack and J. Meadows, Entering BASIC (Chicago: Science Research Associates, 1973). An elementary introductory text for beginners in computer programming.
IBM, Basic Handbook, 3rd ed. (IBM 6361129, 1984). For BASIC and BASICA, but applies to QBASIC as well.
IBM, BASIC Reference, 3rd ed. (IBM 6361134, 1984). For BASIC and BASICA, but applies to QBASIC as well. These BASICs are considerably exended over basic BASIC. In particular, they can handle disk files, sound and graphics.
Jef Raskin, BASIC Programming Reference Manual (Cupertino, CA: Apple #A2L0006, 1981). Applesoft, like the IBM BASIC, is considerably extended.
Caryl Richardson, The Applesoft Tutorial (Cupertino, CA: Apple #A2L0018, 1981).
Composed by J. B. Calvert
Created 1 September 2004
Last revised 9 September 2004