GOOC - Game Oriented Object C

GOOC - Game Oriented Object C

Next: The do Statement, Previous: The if and unless Statements, Up: Statements
[Contents]

8.4. The while and until Statements

8.4. The while and until Statements

The while and until statements are loop statements with an exit test at the beginning of the loop. Here is the general form of a while statement: while (test) statement The while statement first evaluates test. If test evaluates to true, statement is executed, and then test is evaluated 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 while or until loop to exit prematurely.

You can also optionally define variables within the condition. This is useful if you want to compare against a value that is only relevant within the loop. The scope of this variable lasts until the loop it was declared in is over. while (var foo = 0; foo < 50.0) { x += 1.0 foo += x >> 1 }