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

EXIT

Keyword Abbreviation Token (hex) Version(s) Classification
EXIT EX{Shift+I} ED 3.5, 7.0 Command and Statement

  Syntax  
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
statement
Command or
Statment
all Inside (during) the loop
otherCode
Command or
Statement
all   Outside (after) the loop
 
 
  Purpose  
Program flow control.  Escape from the body of DO/LOOP.

 
  Remarks  
The EXIT statment may appear more than once (contrary to the shown Syntax) in the body of a DO...LOOP construct.  Without an EXIT, the loop will run forever if both startTest and endTest are omitted.  (In some cases, NEXT or RETURN may abort DO/LOOP; but the starting FOR/GOSUB must have occured before the DO/LOOP block.)
 
See the entry for DO for more details; this page concentrates on EXIT 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 EXIT (or more typically LOOP) matches the most recent DO.  If EXIT occurs when no DO is on the BASIC stack, then a misleading LOOP WITHOUT DO ERROR is generated (it should say EXIT without DO).
 
If a condition "fails" or EXIT is executed, BASIC removes the matching DO entry from the BASIC stack and proceeds with otherCode (the code following LOOP, if any).  In these cases, a matching LOOP keyword must be found or a LOOP NOT FOUND ERROR occurs.
 
Attempts to provide a parameter to EXIT will generate SYNTAX ERROR.
 
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 (but it doesn't).  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.  A conditional LOOP may sometimes be "needed" because CBM BASIC does not have a "CONTINUE" keyword (there is CONT, but it is for debugging, not DO/LOOP).  The only alternative is GOTO which is a bit ironic since DO/LOOP seems designed to avoid "speghetti code" of GOTO.
 
If you can avoid the conditional LOOP (or place it after EXIT), then you should find that EXIT is very handy.  The use of EXIT in DO/LOOP makes for a cleaner escape from a block of code than GOTO or tricks with NEXT.  On the other hand, EXIT is generally slower than GOTO because EXIT must scan all tokens (well, up to REM) of every line until a matching LOOP is found (in case a nested DO/LOOP is present) while GOTO quickly jumps from line-to-line to find the target.  On the third hand, EXIT is more flexible because the resume point (the statement following LOOP) may be anywhere in a line but GOTO can only resume at the beginning of a line.
 
Example:
NEW

READY.
10 DO
20 : X = X+1
30 : PRINT X;Y
40 : IF X>1 THEN EXIT
50 Y = Y+1 : LOOP : PRINT "GOTO CAN'T GET HERE"
RUN
 1  0
 2  1
GOTO CAN'T GET HERE

READY.
 
  Compare With  
  See Also  

© H2Obsession, 2014
Comments