Blitz3D+ Command Reference

While condition

Parameters

condition - any expression; zero counts as false, anything else as true

Description

Repeats a block of code for as long as a condition stays true.

The block runs between the While line and its Wend, and the condition is checked at the top before every pass - including the very first one. That is the important part: if the condition is already false when you arrive, the body never runs at all. Use While when "maybe zero times" is a sensible answer, such as draining a queue that might already be empty.

It is also the shape of the classic Blitz main loop:

While Not KeyHit(1)
    UpdateGame()
    DrawGame()
    Flip
Wend

The condition is a plain number underneath, so zero is false and anything else is true. Join tests with And and Or, but remember they do not short-circuit - both sides are always worked out.

Something inside the loop has to be able to change the answer, or you have written an infinite loop. If the natural exit sits in the middle of the body rather than at the top, use Exit instead of contorting the condition.

Where the body must run at least once before you can test anything, Repeat ... Until is the better fit.

See also: Wend, Repeat, Exit, For, If.

Example

; While Example
; -------------

; While tests its condition BEFORE each pass - recharge until full
shield=25
Print "Shield generator online. Shield at "+shield+"%"

While shield<100
    shield=shield+15
    If shield>100 Then shield=100
    Print "Recharging ... shield at "+shield+"%"
Wend

; If the shield had started at 100, the loop body would never have run
Print "Shields fully charged!"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index