-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-04-control-structures.html
764 lines (605 loc) · 29.1 KB
/
05-04-control-structures.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<!doctype html>
<html lang="en">
<head>
<!--
This Amos Professional Manual is written by asymetrix for the Amiga community and should stay completely FREE FOREVER.
Created 2008. :)
It was created from the original AMOS Professional Manual by Europress Software Ltd.
It has been updated by Fredrik Rambris.
-->
<title>Control Structures - AMOS Professional Manual</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="keywords" content="Amos Professional, Amiga, Programming, Basic, Francois Lionet, Europress Software Ltd, Amos, computing, code, AmigaDOS">
<meta name="author" content="asymetrix,Fredrik Rambris">
<link rel="GitHub" href="https://github.com/fredrik-rambris/amospromanual">
<meta property="og:site_name" content="AMOS Professional Manual">
<meta property="og:image" content="https://amospromanual.dev/images/cover.jpg">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="canonical" href="https://amospromanual.dev/05-04-control-structures.html">
</head>
<body>
<section>
<h1>Control Structures</h1>
<br>
<P>There is a traditional group of instructions that allow computer programs to make
decisions. They are usually known as control structures. This Chapter explains how AMOS Professional
takes (lie best of these traditions and uses them to give your Amiga a logical brain.</P>
<h3 class="command" id="str-goto">GOTO</h3>
<p><i>structure: jump to a specified place in the program</i><BR>
<b>Goto</b> label<BR>
<b>Goto</b> line number<BR>
<b>Goto</b> expression</P>
<P>
A computer program that can only obey a list of instructions one after the other is a
very limited computer program indeed. One way of forcing programs to jump to specified locations is to
use the old fashioned GOTO structure, followed by a target destination. In AMOS Professional,
these destinations can be a label, a line number or a variable. These are explained in
<a href="05-01-the-bare-bones.html">Chapter 5.1</a>.</P>
<P>
<b>label</b> markers can consist of names that use any string of letters or numbers, as well
as the underscore character "_", and they must be ended with the colon character ":" as follows:</P>
<code class="prefix edit">Print "Jump in two seconds" : Wait 100
Goto LABEL_MARKER
Wait 180000 : <comment>Rem Wait one hour</comment>
LABEL_MARKER:
Print "Now is the time to jump!"
</code>
<P>
<b>Numbers</b> may be used to identify specific lines, and the program can be commanded to
GOTO one of these optional markers, like this:</P>
<code class="prefix edit">Goto 5
Print "I am being ignored"
5 Print "I am line 5"
</code>
<P>
It should be obvious that these identification numbers have nothing to do with the
number of lines in a program, but they may still lead to confusion. Labels are much easier to
remember and to locate.</P>
<P>
<b>Expressions</b> can also be used for this purpose, and the expression may be any string or
integer. Strings hold the name of a label, and integers return a line identification number. Here
is an example:</P>
<code class="prefix edit">BEGIN:
Goto "BED"+"2"
End
BED1:
Print "This Bed will never be used"
Bed2:
Print "Welcome to Bed Two!"
Wait 20
Goto BEGIN
</code>
<h3 class="command" id="str-gosub">GOSUB</h3>
<p><i>structure: jump to a sub-routine</i><BR>
<b>Gosub</b> label<BR>
<b>Gosub</b> number<BR>
<b>Gosub</b> expression</P>
<P>
Packages of program instructions that perform a specific task can be thought of as
"routines". When such routines are split into smaller packages of instructions, they can be thought of
as "sub-routines". GOSUB is another antiquated command, and is used to perform a jump to a
sub-routine. In fact, GOSUB is made redundant by the AMOS Professional procedure system, but
it can be useful for STOS users who want to convert programs.</P>
<P>As with GOTO, there are three alternative targets for a GOSUB instruction: labels, line
numbers or expressions.</P>
<P>
To make sub-routines easier to spot in your program listings, it is good practice to
place them at the end of the main program. A statement such as EDIT or DIRECT should also be used to end
the main program, which prevents AMOS Professional from executing any GOSUBs after the
main program has finished.</P>
<h3 class="command" id="i-return">RETURN</h3>
<p><i>instruction: return from a sub-routine called by GOSUB</i><BR>
<b>Return</b></P>
<P>
When a program obeys a GOSUB instruction, it must be instructed to RETURN to the main
program after the sub-routine has been executed. It should be noted that a single GOSUB
statement can be linked to several RETURN commands, allowing exits from any number of
different points in the routine, depending on the circumstances. After the RETURN, a jump
is made back to the instruction immediately after the original GOSUB. For example:</P>
<code class="prefix edit">Print "I am the main program"
For N=1 To 3
Gosub TEST
Next N
End
TEST:
Print "Here we go GOSUB" : Wait 50
Print "Number =";N
Return
</code>
<h3 class="command" id="i-pop">POP</h3>
<p><i>instruction: remove RETURN information</i><BR>
<b>Pop</b></P>
<P>Normally you cannot exit from a GOSUB statement using a standard GOTO, and this may be
inconvenient. For example, there could be an error that makes it unacceptable to return to
the</P>
<P>
program exactly where you left it. In such circumstances, the POP command can be used to
remove the return address generated by a GOSUB, allowing you to leave the sub-routine
without the final RETURN statement being executed. For example:</P>
<code class="prefix edit">Do
Gosub THERE
Loop
HERE:
Print "I've just popped out!"
Direct : <comment>Rem No risk of accidental subroutine</comment>
THERE:
Print "Hello There!"
If Mouse Key Then Pop : Goto HERE
Return
</code>
</section>
<section id="01-decision-making">
<h2>Decision making</h2>
<p>The command words used in the decision making process have very similar meanings in
AMOS Professional as they do in normal English.</P>
<h3 class="command" id="str-if">IF</h3>
<h3 class="command" id="str-then">THEN</h3>
<P><i>structure: choose between alternative actions</i><BR>
<b>If</b> conditions <b>Then</b> statements</P>
<P>
The IF ... THEN structure allows simple decisions to be made within a program, so IF a condition
is true THEN the computer decides to take a particular course of action. If the condition
is not true, the machine does something else. For example:</P>
<code class="prefix edit">NIGHT=12
DAY=12
Print "What time is it now?" : Wait 150
If NIGHT=DAY Then Goto BED
Print "Time I bought a watch"
Goto WATCHMAKER
BED:
Print "I think it is bed time"
WATCHMAKER:
</code>
<h3 class="command" id="str-and">AND</h3>
<h3 class="command" id="str-or">OR</h3>
<P><i>structures: qualify a condition</i><BR>
<b>If</b> condition <b>And</b> condition <b>Then</b> statement<BR>
<b>If</b> condition <b>Or</b> condition <b>Then</b> statement</P>
<P>
The list of condition; in an IF ... THEN structure can be any list of tests, including AND
and OR. Try changing the conditions of the last example with either of the following lines:</P>
<code class="prefix edit">If NIGHT=DAY And NIGHT<>12 Then Goto BED</code>
<br>
<code class="prefix edit">If NIGHT<DAY Or NIGHT=12 Then Edit</code>
<h3 class="command" id="str-else">ELSE</h3>
<p><i>structure: qualify a condition</i><BR>
<b>If</b> condition <b>Then</b> statement1 <b>Else</b> statement2</P>
<P>ELSE is also understood when making decisions, as to what action should be taken,
depending on conditions. So the last example could be changed to something like this:</P>
<code class="prefix edit">If NIGHT+1=DAY Then Goto BED Else Shoot</code>
<P>
The alternative choice of statements in this sort of structure must be a list of one or
more AMOS Professional instructions. Also remember to include a separate GOTO command if you want to
jump to a label or a numbered line, otherwise the label will be treated as a procedure
name and it could possibly generate an error. For example:</P>
<code class="prefix ex">If NIGHT=1 Then Goto BED: <comment>Rem This is perfect</comment></code>
<br>
<code class="prefix ex">If NIGHT=1 Then BED: <comment>Rem This looks for a BED procedure</comment></code>
<P>
An IF ... THEN statement is limited to a single line, of a listing, which is not very
satisfactory to an AMOS Professional programmer. This technique has been superseded by a "structured test",
where IF is used to trigger off a whole range of instructions, depending on the outcome of
a single decision.</P>
</section>
<section id="02-structured-tests">
<h2>Structured tests</h2>
<h3 class="command" id="str-end-if">END IF</h3>
<p><i>structure: terminate a structured test</i><BR>
<b>If</b> structured test <b>End If</b></P>
<P>
In a structured test, each test is set up with an IF and ended with a matching END IF,
but under no circumstances can a THEN be used anywhere inside such a test! The statements in a
structured test are separated by colons on any particular line, as usual, but can extend
over any number of lines in your listing, as required. Look at this old fashioned schematic line:</P>
<code class="prefix ex">If condition=true Then Goto Label1 Else Label2</code>
<P>This may now be replaced by the alternative structured test format:</P>
<code class="prefix ex">If condition=true : Goto Label1 : Else Goto Label2 : End If</code>
<P>Here is a working example:</P>
<code class="prefix edit">Input "Type values A,B and C: ";A,B,C
If A=B
Print "A equals B";
Else
Print "A is not equal to B";
If A<>B And A<>C
Print "or to C"
End If
End If
</code>
<P>Note how each IF statement must be paired with a single END IF to inform AMOS
Professional exactly which group of instructions is to be executed inside the test.</P>
<h3 class="command" id="str-else-if">ELSE IF</h3>
<p><i>structure: allow multiple structured tests</i><BR>
<b>If</b> condition <b>Else If</b> multiple conditions ... <b>Else</b> statement <b>End If</b></P>
<P>
This allows multiple tests to be performed. ELSE IF must be used within a normal IF ...
END IF statement, and the only rule to remember is that there must be one ELSE just before the
END IF. This sort of test waits for an expression, and if the expression is True, then what comes
after it is executed. Here is an example:</P>
<code class="prefix ex">If A=1
Print "A=1"
Else If A=2
Print "A=2"
Else If A=3
Print "A=3"
Else
Print "Something Else"
End If
</code>
<P>If necessary, an entire test can be placed in a single line, as follows:</P>
<code class="prefix ex">If A=1 : Print "A=1" : Else If A=2 : Print "A=2" : Else : Print "Something Else" : End If<BR></code>
<P>
When taking logical decisions, your Amiga understands the following character symbols, which
are used as a form of short-hand:</P>
<PRE>
<b>Symbol Meaning</b>
= equal to
<> not equal to
> greater than
> less than
>= greater than or equal to
<= less than or equal to
</PRE>
<P>There are also three functions that can be called during the decision making process.</P>
<h3 class="command" id="fn-true">TRUE</h3>
<h3 class="command" id="fn-false">FALSE</h3>
<P><i>functions: hold value of -1 (True) and zero (False)</i><BR>
value=<b>True</b><BR>
value=<b>False</b></P>
<P>
In all the conditional operations such as IF ... THEN and REPEAT ... UNTIL, the value
of -1 is used to represent TRUE, and the value of 0 is used to represent FALSE. A value of either
-1 (True) or 0 (False) is produced every time a test is made to satisfy a condition.</P>
<h3 class="command" id="str-not">NOT</h3>
<p><i>structure: toggle binary digits</i><BR>
value=<b>Not</b> digits</P>
<P>NOT is used to swap over every digit in a binary number from a 0 to a 1, and vice
versa. For example:</P>
<code class="prefix edit">Print Bin$(Not%11110000,8)</code>
<P>Since -1 (True) can be expressed in binary as %1111111111111111, then NOT TRUE must be
equal to FALSE, and a logical NOT operation is achieved.</P>
<h3 class="command" id="str-swap">SWAP</h3>
<p><i>structure: swap the contents of two variables</i><BR>
<b>Swap</b> a,b<BR>
<b>Swap</b> a#,b#<BR>
<b>Swap</b> a$,b$</P>
<P>Use the SWAP command to swap over the data between any two variables of the same type.
For example:</P>
<code class="prefix edit">A=10 : B=99: Print A,B
Swap A,B : Print A,B
</code>
</section>
<section id="03-using-loops">
<h2>Using loops</h2>
<p>To write a separate routine for dozens of logical choices, and to end up with dozens of
END IFs is not only messy, but also extremely tedious. AMOS Professional offers all of the
expected programming short-cuts to allow sections of code to be repeated as often as necessary.
These repeated parts of programs are known as "loops".</P>
<h3 class="command" id="str-do">DO</h3>
<h3 class="command" id="str-loop">LOOP</h3>
<P><i>structure: keep repeating a list of statements</i><BR>
<b>Do</b><BR>
list of statements<BR>
<b>Loop</b></P>
<P>
This pair of commands will loop a list of AMOS Professional statements forever, with DO
acting as the marker position for the LOOP to return to. Both the DO and LOOP should occupy their
own lines, as follows:</P>
<code class="prefix edit">Do
Print "FOREVER AND": Wait 25
Loop
</code>
<h3 class="command" id="str-exit">EXIT</h3>
<P><i>structure: break out of a loop</i><br>
<b>Exit</b><br>
<b>Exit</b> number</p>
<P>
EXIT forces the program to leave a loop immediately, and it can be used to escape from
all the types of loop employed in AMOS Professional, such as FOR ... NEXT, REPEAT ... UNTIL,
WHILE ... WEND and DO ... LOOP. Any number of loops may be nested inside of one another,
and when used on its own, EXIT will short-circuit the innermost loop only. By including an
optional number after EXIT, that number of nested loops will be taken into account before
the EXIT is made, and the program will jump directly to the instruction immediately after the
relevant loop.</P>
<P>For example:</P>
<code class="prefix edit">Do
Do
Input "Type in a number";X
Print "I am the inner loop"
If X=1 Then Exit
If X=2 Then Exit 2
Loop
Print "I am the outer loop"
Loop
Print "And I am outside both loops!"
</code>
<h3 class="command" id="str-exit-if">EXIT IF</h3>
<p><i>structure: exit from a loop depending on a test</i><BR>
<b>Exit If</b> expression<BR>
<b>Exit If</b> expression,number</P>
<P>
It is often necessary to leave a loop as a result of a specific set of conditions, and
this can be simplified by using the EXIT IF instruction. As explained above, in conditional operations,
the value -1 represents True, whereas a zero represents False. After using EXIT IF, an
expression is given which consists of one or more tests in standard AMOS Professional format. The EXIT
will only be performed IF the result is found to be -1 (True).</P>
<P>
As before, an optional number can be given to specify the number of loops to be jumped
from, otherwise only the current loop will be aborted. For example:</P>
<code class="prefix edti">While L=0
A=0
Do
A=A+1
For X=0 To 100
Exit If A=10,2 : <comment>Rem Exit from DO and FOR loops</comment>
Next X
Loop
Exit 1: <comment>Rem Exit from WHILE loop</comment>
Wend
</code>
</section>
<section id="04-conditional-loops">
<h2>Conditional loops</h2>
<h3 class="command" id="str-while">WHILE</h3>
<h3 class="command" id="str-wend">WEND</h3>
<P><i>structure: repeat loop while condition is true</i><BR>
<b>While</b> condition<BR>
list of statements<BR>
<b>Wend</b></P>
<P>
This pair of commands provides a convenient way of making the program repeat a group of
instructions all the time a particular condition is true. WHILE marks the start of this
loop, and the condition is checked for a value of -1 (True) from this starting position through to
the end position, which is marked by a WEND. The condition is then checked again at every turn of
the loop, until it is no longer true. For example:</P>
<code class="prefix edit">BLAZES:
Print "Please type in the number 9"
Input X
While X=9
Cls : Print X : Wait 50 : Goto BLAZES
Wend
Print "That is not a 9!"
</code>
<P>You are free to use AND, OR and NOT to qualify the conditions to be checked.</P>
<h3 class="command" id="str-repeat">REPEAT</h3>
<h3 class="command" id="str-until">UNTIL</h3>
<P><i>structure: repeat loop until a condition is satisfied</i><BR>
<b>Repeat</b><BR>
list of statements<BR>
<b>Until</b> condition</P>
<P>
Unlike that last example, instead of checking if a condition is true or false at the start
of a loop, the pair of commands makes its check at the end of a loop. REPEAT marks the start and
UNTIL the end of the loop to be checked. This means that if a condition is false at the
beginning of a</P>
<P>
WHILE ... WEND structure, that loop will never be performed at all, but if it is true
at the beginning of a REPEAT ... UNTIL structure, the loop will be performed at least once. Here
is an example that waits for you to press a mouse button:</P>
<code class="prefix edit">Repeat
Print "I can go on forever" : Wait 25
Until Mouse Key<>0
</code>
</section>
<section id="05-controlled-loops">
<h2>Controlled loops</h2>
<p>When deciding how many times a loop is to be repeated, control can be made much more
definite than relying on whether conditions are true or false.</P>
<h3 class="command" id="str-for">FOR</h3>
<h3 class="command" id="str-to">TO</h3>
<h3 class="command" id="str-next">NEXT</h3>
<P><i>structure: repeat loop a specific number of times</i><BR>
<b>For</b> index=first number <b>To</b> last number<BR>
list of statements<BR>
<b>Next</b> index</P>
<P>
This control structure is one of the programmer's classic devices. Each FOR statement
must be matched by a single NEXT, and pairs of FOR ... NEXT loops can be nested inside one
another. Each loop repeats a list of instructions for a specific number of times, governed by an
index which counts the number of times the loop is repeated. Once inside the loop, this index
can be read by the program as if it is a normal variable. Here is a simple example:</P>
<code class="prefix edit">For X=1 To 7
Print "SEVEN DEADLY SINS"
Next X
</code>
<h3 class="command" id="str-step">STEP</h3>
<p><i>structure: control increment of index in a loop</i><BR>
<b>For</b> index=first number <b>To</b> last number <b>Step</b> size</P>
<P>Normally, the index counter is increased by 1 unit at every turn of a FOR ... NEXT
loop. When the current value exceeds that of the last number specified, the loop is terminated. For
example:</P>
<code class="prefix edit">For DAY=1 To 365
Print DAY
Next DAY
</code>
<P>STEP is used to change the size of increase in the index value, like this:</P>
<code class="prefix edit">For DAY=1 To 365 Step 7
Print DAY
Next DAY
</code>
</section>
<section id="06-forced-jumps">
<h2>Forced jumps</h2>
<p>So far, it has been explained how certain jumps are made to another part of a program by logical
decisions based on whether a situation is true or false. Similar jumps can be made whenever a
particular variable is recognised, in other words, regardless of any other conditions. GOTO and
GOSUB are examples of a "forced" jump.</P>
<h3 class="command" id="str-on">ON</h3>
<p><i>structure: jump on recognising a variable</i><BR>
<b>On</b> variable <b>Proc</b> list of procedures<BR>
<b>On</b> variable <b>Goto</b> list of numbered lines or labels<BR>
<b>On</b> variable <b>Gosub</b> list of numbered lines or labels</p>
<P>
ON can be used to force the program to jump to a pre-defined position when it
recognises a specified variable. Furthermore, jumps can be made to a choice of several positions,
depending on what value is held by the variable at the time it is spotted. ON can force a jump to
any of the following structures.</P>
<P><b>Procedures</b>. When using an ON ... PROC structure, one or more named procedures is used
as the target destination for a jump, depending on the contents currently held by a variable.
Look at the following line:</P>
<code class="prefix ex">On X Proc PROCEDURE1,PROCEDURE2</code>
<P>That is exactly the same as saying:</P>
<code class="prefix ex">If X=1 Then PROCEDURE1
If X=2 Then PROCEDURE2
</code>
<P>
It is important to note that procedures used in this way cannot include any parameters.
If information is to be transferred to the procedure, it should be placed in a <b>global</b>
variable, as explained in <a href="05-05-procedures.html">Chapter 5.5</a>.</P>
<P><b>Goto</b> is used to jump to one of a list of numbered lines, or a label, depending on the
result of an expression. For example:</P>
<code class="prefix edit">Print "Type in a value from 1 to 3"
Input X
On X Goto LABEL1,LABEL2,LABEL3
LABEL1:
Print "Ready"
LABEL2:
Print "Steady"
LABEL3:
Print "Go!"
</code>
<p>
For that to work properly, X must have a value from 1 up to the number of the highest
possible destination. Any other values would cause problems. In fact the third line of that example
is a very economical way of writing the following lines:</P>
<code class="prefix ex">If X=1 Then Goto LABEL1
If X=2 Then Goto LABEL2
If X=3 Then Goto LABEL3
</code>
<P>Now change the third line of the last example to this:</p>
<code class="prefix edit">On X Goto LABEL3,LABEL2,LABEL1</code>
<P>
Gosub. The use of an ON GOSUB structure is identical to ON ... GOTO, except that it
must employ a RETURN to jump back to the instruction immediately after the ON ... GOSUB
statement. Destinations may be given as the name of a label, or the identification number
of a line between 1 and the maximum number of possible destinations.</P>
<P>ON is also used with the ON BREAK PROC structure, as well as ON ERROR GOTO, which are
explained in the relevant sections of the Procedures and Error Handling Chapters of this
User Guide.</P>
<h3 class="command" id="i-every">EVERY</h3>
<p><i>instruction: call subroutine or procedure at regular intervals</i><BR>
<b>Every</b> time <b>Gosub</b> label<BR>
<b>Every</b> time <b>Proc</b> name</P>
<P>The EVERY statement is used to call up a sub-routine or a procedure at regular
intervals,
without interfering with the main program. Simply specify the length of time between every
call, measured in 50ths of a second. Obviously the time taken for a sub-routine or a
procedure to
be completed must be less than the interval time, or an error will be generated.</P>
<P>After a sub-routine has been entered, the EVERY system is automatically disabled. This
means
that in order to call this feature continuously, an EVERY ON command must be inserted into
a sub-routine before the final RETURN statement. Similarly, EVERY ON must be included in a
procedure before returning to the main program with an END PROC. For example:</P>
<code class="prefix edit">Every 50 Proc TEST
Do
Print At(0,0); "Main Loop"
Loop
Procedure TEST
Shared A
Inc A: Print "This is call number ";A
Every On
End Proc
</code>
<h3 class="command" id="i-every-on">EVERY ON</h3>
<h3 class="command" id="i-every-off">EVERY OFF</h3>
<P><i>instruction: toggle regular EVERY calls</i><BR>
<b>Every On</b><BR>
<b>Every Off</b></P>
<P>
As explained, EVERY ON should be used before the relevant sub-routine or procedure has
finished executing. EVERY OFF is the default condition, and is used to disable the
automatic calling process altogether.</P>
</section>
<section id="07-handling-data">
<h2>Handling data</h2>
<h3 class="command" id="str-data">DATA</h3>
<p><i>structure: place a list of data items in a program</i><BR>
<b>Data</b> list</P>
<P>A DATA statement lets you include whole lists of useful information in your programs.
Each item in the list must be separated by a comma, like this:</P>
<code class="prefix ex">Data 1,2,3,4</code>
<P>Also each DATA instruction must be the only statement on the current line, because
anything that follows it will be ignored! Prove that with the following line:</P>
<code class="prefix edit">Read A$: Print A$
Data "I am legal" : Print "But I am not!"
</code>
<P>Data can be "read" into one or more variables, and unlike many Basic languages, AMOS
Professional allows you to include expressions as part of your data. So the following
lines of code are all equally acceptable:</P>
<code class="prefix ex">Data $FF50,$890
Data %11111111,%110011010110
Data A
Label: Data A+3/2.0-Sin(B)
Data "AMOS"+"Professional"
</code>
<P>Examine those lines, and note that the A at Label will be input as the contents of
variable A, and not the character A. The expression will be evaluated using the latest value of A.</P>
<P>
Data statements may be placed at any position in your program, but any data stored
inside an AMOS Professional procedure will not be accessible from the main program. Each procedure
can have its own individual set of data statements, which are completely independent from
the rest of the program.</P>
<!-- section -->
<P> For example:</P>
<code class="prefix edit">EXAMPLE
Read A$: Print A$
Data "I am Main Program Data"
Procedure EXAMPLE
Read B$: Print B$
Data "I am Procedure Data only"
End Proc
</code>
<h3 class="command" id="str-read">READ</h3>
<p><i>structure: read data into a variable</i><BR>
<b>Read</b> list</P>
<P>
When READ loads items of information that have been stored in a DATA statement into a
list of variables, it uses a special marker to jump to the first item in the first DATA statement
of your listing. As soon as that item of data has been read, the marker moves on to the next item
in the list.</P>
<P>It must be remembered that the variables to be read must be of exactly the same type as
the data held at the current position. If you match up one type of stored data with a different
type of variable after a READ command, the appropriate error message will be given. Here is an
example of correct matching:</P>
<code class="prefix edit">N=Rnd(100)
Read A$,B,C,D$
Print A$,B,C,D$
Data "Text string",100,N,"AMOS"+"Professional"
</code>
<h3 class="command" id="str-restore">RESTORE</h3>
<p><i>structure: set the current READ pointer</i><BR>
<b>Restore</b> Label<BR>
<b>Restore</b> LABEL$<BR>
<b>Restore</b> Line<BR>
<b>Restore</b> number</P>
<P>To change the order in which your data is read from the order in which it was
originally stored, you can alter the point where a READ operation expects to find the next DATA statement.
The RESTORE command sets the position of this pointer by referring to a particular label or
line number, and both labels and numbers may be calculated as part of an expression.</P>
<P>For example:</P>
<code class="prefix edit">Restore LAST
Read A$
Print A$
Data "First"
Data "Middle"
LAST:
Data "Last"
</code>
<p>
Each AMOS Professional procedure has its own individual data pointer, so any calls to
the command will apply to the current procedure only.</p>
<p>
RESTORE is one of the AMOS Professional programmer's most useful devices to force the computer
to select information, depending on the actions of the user. It can be used for educational and
business routines as well as adventure and role-playing games.</p>
</section>
<footer>
<a href="05-03-maths.html" rel="prev">Maths</a>
<a href="./">Contents</a>
<a href="14-appendix-g-command-index.html">Index</a>
<a href="05-05-procedures.html" rel="next">Procedures</a>
</footer>
</body>
</html>