Do/until – Remote Processing CAMBASIC User Manual

Page 72

Advertising
background image

Comm ands - 39

DO/UNTIL

Statements

SYNTAX:

D O
.
.
.
UNTIL expression is true

PURPOSE:

To execute a conditional loop structure.

REMARK S:

The DO/ UNT IL statements cause a list of statements to be executed until a condition is met. You
may exit a DO/ UNT IL with the EXIT statement

EXAMPLE:

10 A= - 45
20 DO
30 INC A:PRINT A
50 UNTIL A=0

Nesting DO/ UNT IL loops is permitted. Ca re must be taken in the construct. The following
example illustrates one of the po ssible pitfalls:

10 DO
20 DO
30 INC X
40 UNTIL X=5
50 INC Y
60 UNTIL Y=5

In the “inside” loop beginning at line 20, variable X is incremented until X = 5. Line 50 is then
executed. Since Y is now 1, e xecution br anches to line 20 . T he prev ious value of X was 5, and it is
now incremented to 6. Since this is greater than 5, the inside loop continues until X overflows (a
very long time).

One solution is to add line 45 to reset X each time so the program w ill run properly:

45 X=0

ERROR:

< UNTIL > – if UNTIL encountered without corresponding DO.

Advertising