GOOC - Game Oriented Object C
GOOC - Game Oriented Object C
8.5. The do Statement
8.5. The do Statement
The do statement is a loop statement with an exit test at the end of the loop. Here is the general form of a do + while statement:
do
statement
while (test)
The do statement first executes statement. After that, it evaluates test. If test is true, then statement is executed again. statement continues to execute repeatedly as long as test is true after each execution of statement. If until is used instead of while, then the truth value of test is flipped, similarly to unless.
A break statement will cause a do loop to exit prematurely.
You can also optionally define variables after the do keyword. This is useful if you want to compare against a value that is only relevant within the loop, such as a loop counter. The scope of this variable lasts until the loop it was declared in is over.
do (var i = 0) {
x += 5.0
i += 1.0
} while (i < 10.0)
The caveat in this particular (but quite common) scenario is that i will not be incremented in the case of a continue statement. This can be amended using the step block. The step block is a block of code placed before the condition expression, which always runs before the condition is tested, like this: or at least it should...
do (var i = 0) {
x += 5.0
} while ({i += 1.0}; i < 10.0)
This is, in turn, similar to a for statement in C.