| 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 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 |
DO [ { UNTIL | WHILE } startTest ] [ : statement ] ... [ statement : ] ... [ EXIT ] [ : statement ] ... [ statement : ] ... LOOP [ { UNTIL | WHILE } endTest ] [ : otherCode ] ...
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. Example:
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.)
© H2Obsession, 2014 |