Home
CBM
  ASCII-X
  BASIC
    Disk Commands
    Enter RUN mode
    Program Format
    Secret Variables
    Variable Format
    Expressions
    Keywords
      (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
      Exit
      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
    Syntax
    Tokens
  C128
  D64plus
  Disk
  Escape Codes
  Hardware
  PCxface
  PETSCII
  Pet2asc
Futurama
IBM PC-AT
Contact
Games
Glossary
Hall of fame
Hall of shame
Miscellaneous
Privacy policy
Programming
Twisty puzzles
KeywordAbbreviationToken (hex)Version(s)Classification
LOOPLO{Shift+O}EC3.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.  End a block of statement(s) and optionally evaluate endTest before another pass.

 
 Remarks 
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.
 
Example:
TI$="000000" : DO WHILE TI<10 : PRINT TI : LOOP
 0           values may differ slightly on different machines
 2
 5
 8

READY.
NEW

READY.
10 DO WHILE X<2
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
60 LOOP
70 PRINT X;Y
RUN
 1  0
 SKIP Y+1? N
 2  1
 SKIP Y+1? Y
 2  1

READY.
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