COMPUTE! ISSUE 37 / JUNE 1983 / PAGE 204
TI
Structured BASIC
Steven M. Ruhl
There has been a debate for years
about the merits of "structured programming." In essence, this approach
stresses certain rules and conventions which (according to its
supporters) result in better, more easily understood program listings
and more efficient programming in general. This discussion of
structured programming, as applied to the TI-99/4A, should let you
decide this issue for yourself.
Structured programming can help some programmers make fewer errors, and
can make complex programs easier to modify. Structured programming
involves planning and organization so that a program flows logically
from one step to another. Some structured programming enthusiasts even
outlaw the use of the GOTO statement, since GOTO interrupts the
straightforward flow of a program, and may lead to confusing design.
Structured programming also makes liberal use of REM
statements, so someone reading a program listing can understand the
program's logic easily.
Structured programmers often employ modular
programming - breaking a program into a series of problems, and solving
each separately. Most programs, for example, can be broken down into
four parts: initialization, input, processing, and output. Let's look
at each of these parts in turn. The highest-level module in a program
is the most general, and it controls the modules below it; as the
program progresses, each succeeding module performs more specific tasks.
We can use a simple example to illustrate structured
programming. Program 1 asks for seven numbers and prints their sum.
Program 2 accomplishes the same task, but it does it according to the
rules of structured programming. Let's see how it works.
Initialization
In the initialization module, the variables to be used in the program
are defined in REM statements, and are initialized or dimensioned if
necessary. The REM statements are indented to distinguish them from
normal program statements. The blank REM lines separate program modules.
On some computers, variables must be set to zero at
the beginning of a program - SUM= 0, for example. The TI-99/4A,
however, clears all variables each time a RUN command is entered, so we
needn't worry about that phase of initialization.
If you are using array variables, they may need to
be DIMed, and the initialization module is the place to do it. DIM
statements, which tell the computer how much space to reserve for your
array, can be executed only once for each array variable, and must be
executed before any other reference is made to the array.
Since we are adding seven numbers, we dimension a
seven-element array in lines 180 and 190. When an array is DIMed, the
computer sets the lower limit of the array subscript to zero. In other
words, DIM N(7) is really an eight-element array composed of the
variables N(0), N(1), N(2), N(3), N(4), N(5), N(6), and N(7).
The OPTION BASE 1 statement in line 180 is a feature
of TI BASIC that tells the computer to make the lower limit of the
array subscript one rather than zero. So, by using OPTION BASE 1, we
eliminate the variable N(0) from our list and end up with a
seven-element array.
Note that in Program 1, the variable N was not
DIMed. In such cases, the TI automatically sets the upper limit of the
array subscript to 10. Program 2 would have worked just as well without
lines 180 and 190, but we include them to provide the documentation
structured programming requires.
Input
Data can be passed to a program in a number of ways, including the
INPUT, READ, DATA, and RESTORE statements. TI Extended BASIC offers a
few other input possibilities: ACCEPT, SIZE, ERASE ALL, and VALIDATE.
In our example, a simple FOR/NEXT loop of INPUT
statements is used to enter the seven numbers to be added. Structured
programmers indent the lines within a FOR/NEXT loop to indicate
(visually) what is being accomplished within the loop.
Once the INPUT is completed, control passes to the
processing module.
Processing
Here again, a simple FOR/NEXT loop is used to add the values of the
seven variables. Program 1 includes the processing statement in its
INPUT loop, a perfectly valid way of handling the problem. The
structured program separates the input and processing functions so that
the tasks performed by each can be more easily understood.
Output
The output module takes the result of the processing module and, in
this case, prints it on the screen. Output also can be sent to
printers, tape, or disk.
Since the purpose of most programs is to provide
some kind of computed information, or output, many programmers begin
their program design with a definition of how that output will appear
on the screen or the printer. After the form of the output has been
determined, the input module can be tailored to produce the kind of
information needed.
In TI BASIC, for example, the colon print separator
can be helpful in formatting output.
PRINT
"HELLO":"THERE"
will cause the two words to be printed on separate lines:
HELLO
THERE
Multiple colons can be used to print blank lines
between output. For example,
PRINT "HELLO":
:"THERE"
would insert a line of space between the words when they are printed.
The same process can be used in TI Extended BASIC, but spaces must be
left between the colons, because Extended BASIC interprets a double
colon as a multistatement line.
Easy Modifications
One main purpose of following the rules of structured programming is to
achieve clarity and understanding. It may take some rewriting to clear
up any rough spots and make the documentation complete. A few months
from now, you may want to use a modified version of your program to
handle another task.
A clearly documented listing can save you the
trouble of relying on your memory when you begin making changes. A
program written in modules can also allow you to transfer these
"subprograms" to your new program without much modification.
The rules are there for you to follow if you wish.
The choice is yours.
Program 1:
Demonstration
Program
100 REM ADD
SEVEN NUMBERS
110 REM
120 FOR I = 1
TO 7
130
PRINT
"ENTER NUMBER ";I
140
INPUT N(I)
150
SUM = SUM +
N(I)
160 NEXT I
170 PRINT "SUM
OF SEVEN NUMBERS ";SUM
Program 2:
Structured
Demonstration Program
100 REM
INITIALIZATION MODULE
110 REM
120 REM
N(1...7) LIST OF SEVEN NUMBERS
130 REM
TO BE
INPUT IN LOOP.
140 REM SUM IS
THE SUM OF THE SEVEN
150 REM
NUMBERS
N(1)...N(7).
160 REM INDEX
IS USED TO CONTROL LOOPING
170 REM
180 OPTION BASE
1
190 DIM N(7)
200 REM
210 REM INPUT
MODULE
220 REM
230 FOR INDEX =
1 TO 7
240
PRINT
"ENTER NUMBER ";INDEX;
250
INPUT
N(INDEX)
260 NEXT INDEX
270 REM
280 REM
PROCESSING MODULE
290 REM
300 FOR INDEX =
1 TO 7
310
SUM =
SUM+N(INDEX)
320 NEXT INDEX
330 REM
340 REM OUTPUT
MODULE
350 REM
360 PRINT "SUM
= ";SUM
370 END