Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎


Keyword Abbreviation Token (hex) Version(s) Classification
LOOP LO{Shift+O} EC 3.5, 7.0 Command and Statement

DO [ { UNTIL | WHILE startTest ] [ : statement ] ...
[ statement : ] ... [ EXIT ] [ : statement ] ...
[ statement : ] ... LOOP [ { UNTIL | WHILE } endTest ] [ : otherCode ] ...
Parameters Type Legal Value(s) Default Value Note(s)
startTest Float    all   Zero is false, any other number is true
endTest Float all Zero is false, any other number is true
Command or
all Inside (during) the loop
Command or
all   Outside (after) the loop
Program flow control.  End a block of statement(s) and optionally evaluate endTest before another pass.

The entire DO...LOOP construct may be on a single line, or span any number of lines (not only 3 lines like Syntax shows).  The statement(s) will be executed an arbitrary number of times; depending on the results of startTest and/or endTest (if either).  The loop will run forever (or until an EXIT is executed) if both startTest and endTest are omitted.
See the entry for DO for more details; this page concentrates on LOOP itself.
The WHILE clause will check if a condition is true, and if not (it "fails") the otherCode following the LOOP (if any) will be executed.  The UNTIL clause is the opposite; it will check if a condition is false, and if not (it "fails") it prevents another pass as well.
DO/LOOP may be nested.  Each DO uses 5 bytes of the BASIC stack and LOOP matches the most recent DO.  If the endTest fails, LOOP removes the matching DO entry from the BASIC stack.  The LOOP keyword is only technically required when an EXIT keyword is executed or a "failed" startTest occurs.  In these cases, a LOOP NOT FOUND ERROR will occur if a matching LOOP is not found.  Although not always strictly required, failure to match each DO with a LOOP will eventually (after multiple occurances) exhaust the BASIC stack and generate OUT OF MEMORY ERROR (a so-called memory leak).
Unlike Visual BASIC or C, a test can be made at both the DO (start) and LOOP (end) parts of a pass in CBM BASIC.
One problem exists which is similar to that of FOR/NEXT.  The problem is that using LOOP in a conditional statement (IF/THEN/ELSE) may foul things up.  This happens when EXIT occurs before the conditional LOOP or the startTest "fails".  In my opinion, EXIT should ignore any LOOP that occurs in a conditional block of code.  Anyway the solution is similar to that of a conditional NEXT, which is to follow the conditional LOOP with GOTO to get to the real/unconditional end of the loop.  The reason for using LOOP in a conditional statement is because there is no "CONTINUE" in CBM BASIC (there is CONT, but it is for debugging, not DO/LOOP).
Unlike the similar NEXT and somewhat related RETURN, which can both resume execution of a stopped program, LOOP can not.  This is because it fails to update a secret variable.  In other words, it fails to Enter RUN Mode.
Although endTest is optional, if present a SYNTAX ERROR occurs if it is not a valid expression or TYPE MISMATCH ERROR if it is a string expression.  If LOOP is encountered but no DO is found on the BASIC stack, a LOOP WITHOUT DO ERROR is generated.
TI$="000000" : DO WHILE TI<10 : PRINT TI : LOOP
 0           values may differ slightly on different machines


20 : X = X+1 : PRINT X;Y
30 : INPUT "SKIP Y+1";A$
40 : IF A$="Y" THEN LOOP : GOTO 70
50 : Y = Y+1
 1  0
 2  1
 2  1

Note if you remove GOTO 70 and run the program again (and give the same answers) you will get LOOP WITHOUT DO ERROR IN 60.  This is because when you answer Y on the second pass, the DO WHILE condition fails and it searches for a matching LOOP.  Unfortunately it matches the conditional LOOP of line 40, and without the GOTO then LINE 50 executes (no big deal) and then LINE 60 executes, which is another LOOP.  But by this time the original DO has been cleared so you get the error.  This problem would not occur if BASIC would simply ignore conditional LOOPs when searching for a match, but since it doesn't work that way, you need GOTO.  (At least in this example, other cases might allow other possibilities.)
  Compare With  
  See Also  

© H2Obsession, 2014