Index
Flow Control

while

While

Description

Beginning of a while loop. The statements between a ‘while’ and a ‘repeat’ should ultimately push a value to the stack that will determine if the loop executes the body of the ‘repeat’ block.

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

Examples

# CONSOLE SCRIPT
5 ->y
while   <-y gt(0)    #is y greater than zero?
repeat              #repeat this section of code
    trace(<-y)
    <-y sub(1) ->y   #subtract 1 from y so we don't end in infinite loop
endwhile
# UNIT SCRIPT
 
# This script increases the max health of the unit over time.
# The script won't be stuck in the loop since it's limited by the unit's
# Health regen rate or "GetUnitHealRate(self)".
 
while eq(GetUnitHealth(self) GetUnitMaxHealth(self))        #If unit's current health is equal to it's max health...
    repeat                                                              #...repeat this section of code.
    SetUnitMaxHealth(self GetUnitMaxHealth 0.5 add)                     #Add 0.5 points of max health to the unit
    SetUnitDebugText(self Concat("Max Health: " GetUnitMaxHealth(self)) #Visual aid for you to know.
endwhile

Index