DO … LOOP

Statement

Syntax

Runs its body once per frame: the frame loop of a classic game. Code before DO runs once first, code after LOOP runs once when EXIT leaves the loop.

[<statements>]
DO
  <statements>
  Sync()
LOOP
[<statements>]

Also written : DO, LOOP

Parts

<statements>Before DO: setup, run once. Between DO and LOOP: the frame body. After LOOP: run once, after EXIT.

Good to know

  • One DO … LOOP per program, at the top level only. Not inside an IF, a loop or a FUNCTION: "DO/LOOP is supported once, at top level only".
  • The body must call Sync(), Swap() or YieldTask() to hand the frame back. Without it the compiler says "DO/LOOP requires SYNC() so the loop can yield a frame".
  • In AGK and DarkBASIC, DO … LOOP is a plain endless loop. Here it is the frame loop. For any other loop, use WHILE or REPEAT. DO WHILE, DO UNTIL, LOOP WHILE and LOOP UNTIL do not exist.
  • CONTINUE in the frame loop skips the Sync() below it. If it fires on every pass, the game freezes. Put Sync() before the CONTINUE.
  • Do not write a FUNCTION main() in a program with a DO loop: the loop is compiled under that name, the two collide, and the loop never runs. Nothing warns you.
  • END inside the loop stops the program and skips the code after LOOP. Wait() and Sleep() are refused after LOOP.

Example

frames = 0Console("start")   ' startDO  frames = frames + 1  IF frames = 3 THEN EXIT  Sync()LOOPConsole("left after " + Str(frames))   ' left after 3
In the manual : The game loop and tasks

Commands that go with it