Home > CBM > BASIC > Keywords > Exit
180 Subpages: (divide), (equal), (less), (minus), (more), (multiply), (plus), (power), Abs, And, Append, Asc, Atn, Auto, Backup, Bank, Begin, Bend, Bload, Boot, Box, Bsave, Bump, Catalog, Char, Chr, Circle, Close, Clr, Cmd, Collect, Collision, Color, Concat, Cont, Copy, Cos, Data, Dclear, Dclose, Dec, Def, Delete, Dim, Directory, Dispose, Dload, Do, Dopen, Draw, Ds, Ds string, Dsave, Dverify, El, Else, End, Envelope, Er, Err, Exp, Fast, Fetch, Filter, Fn, For, Fre, Get, Get num, Getkey, Go, Gosub, Goto, Graphic, Gshape, Header, Help, Hex, If, Input, Input num, Instr, Int, Joy, Key, Left, Len, Let, List, Load, Locate, Log, Loop, Mid, Monitor, Movspr, New, Next, Not, Off, On, Open, Or, Paint, Peek, Pen, Pi, Play, Pointer, Poke, Pos, Pot, Print, Print num, Pudef, Quit, Rclr, Rdot, Read, Record, Rem, Rename, Renumber, Restore, Resume, Return, Rgr, Right, Rlum, Rnd, Rreg, Rspcolor, Rsppos, Rsprite, Run, Rwindow, Save, Scale, Scnclr, Scratch, Sgn, Sin, Sleep, Slow, Sound, Spc, Sprcolor, Sprdef, Sprite, Sprsav, Sqr, Sshape, St, Stash, Step, Stop, Str, Swap, Sys, Tab, Tan, Tempo, Then, Ti, Ti string, To, Trap, Troff, Tron, Until, Using, Usr, Val, Verify, Vol, Wait, While, Width, Window, Xor

KeywordAbbreviationToken (hex)Version(s)Classification
EXITEX{Shift+I}ED3.5, 7.0Command and Statement

 Syntax 
DO [ { UNTIL | WHILE startTest ] [ : statement ] ...
[ statement : ] ... [ EXIT ] [ : statement ] ...
[ statement : ] ... LOOP [ { UNTIL | WHILE } endTest ] [ : otherCode ] ...
 
ParametersTypeLegal Value(s)Default ValueNote(s)
startTestFloat   all Zero is false, any other number is true
endTestFloatallZero is false, any other number is true
statement
Command or
Statment
allInside (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+130 : 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