Page tree
Skip to end of metadata
Go to start of metadata



Introduced in

Version:1.00

Description

Description:Repeatedly executes a code segment, for as long as the loop condition returns true.

If the index variable used in the 'for...forspec' loop exists already, then it is used and modified by the loop:

_i = -1;
for [{_i=1},{_i<4},{_i=_i+1}] do {
  player sidechat str _i;
};
player sidechat str _i; // '_i' has been modified by the loop, and is now 4

If the index variable does not exist before the loop, then it is destroyed at the end of the loop, and is not accessible afterwards:

for [{_i=1},{_i<4},{_i=_i+1}] do {
  player sidechat str _i;
};
if (isNil "_i") then {player sidechat "undefined"}; // '_i' is still undefined

An ongoing loop can be exited using the exitWith command:

for [{_i=0},{_i<11},{_i=_i+1}] do {
  if (_i==3) exitWith {
    player sidechat format["exit loop at %1",_i];
  };
  player sidechat format["loop val: %1",_i];
}; // Outputs 1, 2, 3

It is possible to modify the loop variable within the loop:

for [{_i=0},{_i<11},{_i=_i+1}] do {
  if (_i==3) then {
    _i = 8;
  };
  player sidechat format["loop val: %1",_i];
}; // Outputs 0, 1, 2, 8, 9, 10

While the loop specs normally contain references to an index variable (its start and end value, and the increment command), those definitions do not have to refer to any variable. They can contain any code, or even be empty. Only if an index variable is needed later (for example, in the end or the step segment, or in the code section), does it have to be defined in the start segment.

_i=-1;
for [{},{_i!=10},{_i=round (random 10)}] do {
  player sidechat format["[K] %1",_i];
  sleep .1;
}; // creates random numbers between 0 and 10. Once that number is 10 the loop is exited.

Example of loop specification without any index variable (moves unit w1 around randomly, for as long as it is alive):

for [{removeAllWeapons w1},{alive w1},{w1 setPos (w1 modelToWorld [1-round random(2),1-round random(2),0])}] do {
  player sidechat format["[K] %1",getPos w1];
  sleep .5;
};

Syntax

Syntax:for [{start}, {continue}, {step}] do {code}
Parameters:
  • start: Code - Executes once at the start of the loop. If an index variable is to be used in the loop or the loop specification, it has to be assigned here. Can be empty.
  • continue: Code - As long as this code (or variable) returns true, the loop continues. Once it returns false, the loop exits immediately (without executing the code section). Cannot be empty.
  • step: Code - Executes after each loop. Can be empty, but the loop is only executed once if it is, ignoring the result of the continue parameter.
  • code: Code - Executes once per loop (after the first-time execution of the start code, and before each time the step code runs). Can be empty.
Return Value:Nothing

Examples

Examples:
 for [{_x=1},{_x<10},{_x=_x+1}] do {player sidechat str _x}; // outputs 1 to 9  

Additional Information

See also: for var, while, Control Structures
Multiplayer: 
Problems: 

Notes