Telling Stories in BASIC
What's basic is that you need a program development system to write a computer program. Basically, that's what BASIC is.
-----------------------------------------------------------------------------------
------------------------------------------------
BASIC is one of the simpler and more straightforward among the older programming systems, partly because its statements and operators are closely related to ordinary English. This alone makes it a valuable tool for demonstration.
It is also the originating tongue for Visual Basic. Programs written in BASIC can be converted relatively easily into Visual Basic, even though the latter is object-oriented, event driven, and modular in nature, and the former isn't.
Like all programming languages, BASIC includes (1) language elements, (2) basic expressions using the elements, (3) operators that connect expressions, and (4) instructions.
---------------------------------------------
BASIC has a vocabulary. That vocabulary includes terms such as INPUT, TAN, LOCATE, PRINT, WHILE, COLOR, GOTO, GOSUB, CINT, and END, each of which has a specific meaning. Some of the elements are simple statements, some are functions, and others are commands.
Like all other programming systems, BASIC also contains symbols we normally use for math and logic expressions. It includes the elements: +, -, <, >, =, AND, OR, and IF ... THEN, among others. And it naturally has all of the numbers, letters of the alphabet, and many other characters, having the same general meaning as in ordinary usage as well as in the other programming languages.
----------------------------------------------
An expression is a string of alphanumeric characters that forms either a constant or a variable. The term ‘alphanumeric’ means the expression may contain alphabetic and numeric characters, while a pure number has no alphabetic characters. Therefore, a string contains at least one non-numerical character, otherwise it would be a pure number.
Constants
Constants are values used during execution of a program and could be a string of alphanumeric characters or a pure number. Here are a few examples of string and number constants.
|
String Constants |
Number Constants |
|
"Good afternoon." "$30,000.00" "3-all tie" "Column B" "Your Name:" |
235,726 -49.34 7361.329 .5002 1,235,197 |
Sample of string constants and numeric constants.
Notice that string, or non-numerical, constants are always expressed using quotation marks. That’s BASIC’s way of telling the computer what kind of "constant" it's dealing with.
Variables
An expression might also be a variable, a name that stands for alphanumeric values used in a program. That is, a variable may represent either a pure number or a string of characters. The distinguishing mark between a number variable and a string variable is the dollar sign $, which is attached to the end of string variables. The dollar sign is part of the encoding. For example, the string variable A$ could have the value:
A$ = "The weather is great!"
The dollar sign declares that the variable is non-numerical and the quotation marks verify that the value is a string value.
For number variables, though, you don’t use the tag. For instance, the variable A is understood to be a number variable and could be expressed as having, let’s say, the value:
A = 437.
-----------------------------------------------
Operators are expressions that perform mathematical or logical operations on values. In BASIC, just like in other languages, there are four operator categories: arithmetic, relational, logical, and functional.
Arithmetic
Arithmetic operators are the normal operations of arithmetic -- +, -, etc.. Their job is to transform numerical values into other numerical values. The operators include addition, subtraction, multiplication, division, negation, and exponentiation.
A major characteristic of these operations is: they don't have the same level of dominance. That is, within a given expression, they can’t be performed in arbitrary order. If more than one operator appears in a single expression, they must be performed according to their order of precedence, one before the other. In the following table the operators are listed in order of their dominance, top to bottom. For example, -x has to be performed before X/Y, if they both appear in the same expression.
|
Operator |
Operation |
Sample Expression |
|
^ - * / + - |
Exponentiation Negation Multiplication Division Addition Subtraction |
X^Y -X X*Y X/Y X+Y X-Y |
Operators and their corresponding operations.
There is this caveat, however: The normal precedence, or scope, of operators in algebraic expressions may be bypassed by using parentheses to isolate operations and, in effect, treat them as separate expressions. You can see the effect on the order of precedence in the following table of algebraic expressions.
|
Algebraic Expression |
BASIC Form |
Order of Operations (read left to right) |
|
X + 2Y (X + 2)Y X - Y/2 ( X - Y)/2 X2 + Y/2 (X2 + Y)/2 |
X + 2*Y (X + 2)*Y X - Y/2 (X - Y)/2 X^2 + Y/2 (X^2 + Y)/2 |
*, + +, * /, - -, / ^, /, + or /, ^, + ^, +, / |
Algebraic expressions and the order of operations of their operators.
For instance, in the third line, you have to divide Y by 2 before you subtract its result from X.
Relational
Relational operators are those that compare values, one against another. For example, one may be less than another. Or one might be greater than another, or equal to it. Normally it is numbers that are compared this way; but the relations can also be applied to strings, simply by ranking the strings, using numerical equivalents of the characters in the strings.
As you'll see in more detail, below, relational operations are useful when making decisions about the flow of commands in a program. By comparing values one to another you can decide which of alternate paths to follow in making the decisions.
Logical
Logical operations in any expression are performed only after all arithmetic and relational operations are completed, and they themselves are performed according to precedence, relative to each other. Just as with arithmetic operations, however, the order of logical operation may be changed by using parentheses.
Logical operators can be combined with relational operators to decide between alternate paths of program flow, forming what are called control structures. (Generally, a control structure imposes a condition on the order in which instructions are performed, but control structures may also serve other purposes. For example, the language system, ToolBook, uses a control structure when you want the PRINT command to send information to a printer or plotter.)
Since we are dealing with sequential computers, the natural flow of control in a program is sequential. This is to say that processing begins with the first instruction in the program list and continues sequentially from one instruction to the next in the order in which they are written. For most programs, however, the simple sequential flow isn't enough. What is usually needed, in addition, is branching. With branching, the computer continues to sequence, but now the steps can be made along different paths.
Branching fulfills the requirement for alternative choices in decision-making. Branch points are points of decision, and the logical operator provides the criteria for the branching choices.
The logical operator connects multiple relations together, performs a logical comparison on different pieces of information, and makes a decision based on the truth or falsity of the comparisons. In the following, for example, the test to establish the next program instruction is performed by the IF ... THEN operator:
IF A < 20 THEN GOTO 50
This instruction states that if the numerical variable A has a value less than 20, then the next instruction to be executed is at location 50 of the program. Otherwise, no special action is taken and the instruction to be executed next is the one next in the sequential order -- the next one in the list.
Functional
Like all other languages, BASIC includes a number of so-called "intrinsic" functions that can be performed on a value, or operand, to return a value. For example, it includes functions like square root, or trigonometric functions like sine and cosine. A functional operator is used to call one or another of these predetermined operations into use to do the calculations.
----------------------------------------------
Instructions in the language are formed by stringing basic elements together using syntactical rules, just as in ordinary language. One example is the branch-point instruction, mentioned above. There are also statements like:
Let x = 4
PRINT "The weather is clowdy."
The keyword 'Let' is optional, but the first expression is an assignment statement. It tells the computer that the variable x has the value 4. The second tells the computer to print out the string within the quotation marks precisely as it is given, whether or not the spelling is correct. The output is simply:
The weather is clowdy.
Some of the many instructions are listed in the next table. Be advised, however, that the formats for the instructions are only representative of the actual formats and therefore are generally not complete. You would have to refer to the appropriate BASIC manual to get the full particulars. They can get very detailed. Indeed, when you use instructions from any programming language, you should consult its manual to get full descriptions of their use.
|
Instruction Format |
Action |
|
BEEP CINT (x) CIRCLE (x,y),radius(,attributes) COLOR (foregr'nd) (,B'ckgr'nd) END FOR <var>=x TO y (STEP z) : NEXT GET (#) <file no.>(,record>) |
Sound the speaker at 800-Hz Convert x to nearest integer Draw circle with center at x,y Select fore- & backgr'nd colors End program execution
Perform instructions in a loop
Read record from random file |
|
GOSUB <line number> : RETURN IF <expression> THEN <stat'm'nt> INPUT <prompt.>;<var. list> LOCATE y, x LINE ((x1, y1) - (x2, y2)) |
Branch and return
Decide branch of program flow Allow input from monitor Position cursor: line y, col. X Draw line from point 1 to 2 |
|
PEN ON, OFF PSET (x, y)(,attribute) PRINT (<expressions>) REM <remarks> RUN (<line number>) SCREEN (mode) |
Read the light pen Store data to graphics memory To output data at a terminal Allow explanatory remarks Execute program in memory Set screen attributes |
|
STICK (n) WHILE <expression> : WEND |
Return x, y coord’s of joysticks
Execute statements in a loop |
|
WRITE (<expressions>) |
Output data at the terminal |
Sample instructions and their formats.
-----------------------------------