Hardware Reference
In-Depth Information
Logical constructs
Modern versions of BASIC provide us with a number of other useful constructs
which can be instrumental in the production of efficient structured code. As an
example, a somewhat more elegant delay procedure can be produced using the
WHILE
...
WEND
construct. This routine uses a single variable rather than the
two that were required in the
FOR
...
NEXT
construct used earlier.
REM Delay procedure
SUB Delay(lim%)
WHILE lim% > 0
lim% = lim% - 1
WEND
END SUB
The condition in the
WHILE
stated is tested and, as long as it remains true (i.e.
evaluates to non-zero), the code within the loop will be repeated. It should,
perhaps, be stated that there is no particular advantage in using
WHILE
...
WEND
in this simple delay subroutine and a straightforward
FOR
...
NEXT
loop would,
in practice, be perfectly adequate!
The
DO
...
LOOP
construct offers an even more powerful alternative to
FOR
...
NEXT
and
WHILE
...
WEND
. Several forms of
DO
...
LOOP
structure are available
with tests for the loop condition at the start of the loop (
DO WHILE
...
and
DO
UNTIL
...
LOOP
) and tests at the end of the loop (
DO
...
LOOP WHILE
and
DO
...
LOOP UNTIL
). The logic of these constructs is contrasted in Figures 6.2 and 6.3,
Figure 6.2
Flowcharts illustrating the logic of
DO WHILE
...
LOOP
and
DO
UNTIL
...
LOOP
structures. (a)
DO WHILE
...
LOOP
; (b)
DO UNTIL
...
LOOP