Sinclair QL Beginner's Manual page 96

Hide thumbs Also See for QL:
Table of Contents

Advertisement

CHAPTER 14 – PROGRAM STRUCTURE
In this chapter we go again over the ground of program structure : loops and decisions or selection.
We have tried to present things in as simple a way as possible but SuperBASIC is designed to cope
properly with the simple and the complex and all levels in between. Some parts of this chapter are
difficult and if you are new to programming you may wish to omit parts. The topics covered are:
Loops
Nested loops
Binary decisions
Multiple decisions
The latter parts of the first section, Loops, get difficult as we show how SuperBASIC copes with
problems that other languages simply ignore. Skip these parts if you feel so inclined but the other
sections are more straightforward.
LOOPS
In this section we attempt to illustrate the well known problems of handling repetition with simulations
of some Wild West scenes. The context may be contrived and trivial but it offers a simple basis for
discussion and it illustrates difficulties which arise across the whole range of programming
applications.
EXAMPLE 1
A bandit is holed up in the Old School House. The sheriff has six bullets in his gun. Simulate the firing
of the six shots.
Program 1
100 REMark Western FOR
110 FOR bullets = 1 TO 6
120
PRINT "Take aim"
130
PRINT "Fire shot"
140 END FOR bullets
Program 2
100 REMark Western REPeat
110 LET bullets = 6
120 REPeat bandit
130 PRINT "Take aim"
140 PRINT "Fire shot"
150 LET bullets = bullets - 1
160 IF bullets = 0 THEN EXIT bandit
170 END REPeat bandit
Both these programs produce the same output:
Take aim
Fire a shot
Is printed six times
If in each program the 6 is changed to any number down to 1 both programs still work as you would
expect. But what if the gun is empty before any shots have been fired?
EXAMPLE 2

Advertisement

Table of Contents
loading

Table of Contents