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 variable has not reached its end value yet.

The 'for...var' loop creates a new instance of the loop variable. If a variable of the same name already existed in the script, then this existing one is not affected by the loop:

_i = -1;
for "_i" from 0 to 3 do {
  player sidechat str _i;
};
player sidechat str _i // '_i' is still -1, the loop did not affect it

The loop variable itself is destroyed at the end of the loop, and not accessible afterwards:

for "_i" from 0 to 3 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" from 0 to 10 do {
  if (_i==3) exitWith {
    player sidechat format["exit loop at %1",_i];
  };
  player sidechat format["loop val: %1",_i];
}; // Outputs 0, 1, 2, 3

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

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

Syntax

Syntax:for loopvar from start to end do {code}
Parameters:
  • loopvar: String - Name of index variable to modify within the loop (must start with underscore, be all lowercase, and be enclosed in double quotes).
  • start: Number - Initial value to start loop with.
  • end: Number - Highest value in loop. Once loopvar is equal or greater than this number, the loop is exited.
  • code: Code - Code block to execute each loop cycle.
Return Value:Nothing

Alternative Syntax

Syntax:for loopvar from start to end step step do {code}
Parameters:
  • loopvar: String - Name of index variable to modify within the loop (must start with underscore, be all lowercase, and be enclosed in double quotes).
  • start: Number - Initial value to start loop with.
  • end: Number - Highest value in loop. Once loopvar is equal or greater than this number, the loop is exited.
  • step: Number - Value by which to increment loopvar on ever cycle. Can be negative for decreasing loops (optional, default: 1).
  • code: Code - Code block to execute each loop cycle.
Return Value:Nothing

Examples

Examples:

for "_i" from 1 to 5 do {player sidechat str _i;}; // outputs: 1, 2, 3, 4, 5

for "_i" from 9 to 1 step -2 do {player sidechat str _i;}; // outputs: 9, 7, 5, 3, 1

Additional Information

See also: for forspec, forEach, exitWith, Control Structures
Multiplayer:
Problems:

Notes







Posted on Aug 12, 2014
Kronzky
Be aware that, when using decimals for the 'step' value, the 'loopvar' value will contain rounding errors in V2.0+:


for "_i" from 0 to .5 step .1 do {player sideChat str _i}; 
// V1.x returns 0, 0.1, 0.2, 0.3, 0.4, 0.5
// V2.x+ returns 0, 0.100000001490116, 0.200000002980232, 0.300000011920929, 0.400000005960464, 0.5

To prevent this, it is better to keep the 'step' value an integer, and to then do any calculations within the loop, e.g.

for "_i" from 0 to 5 do {player sideChat str (_i/10)};
// will return 0, 0.1, 0.2, 0.3, 0.4, 0.5 in any version


Posted on 07:26, 26 July 2012 (PDT)
Richard Kissel
Any step increment > 2 will require a limit of round(step/2) to ensure an additional loop is not executed. e.g.,


for "_i" from 0 to 14 step 5 do {}; //will process _i for 0,5,15 unless the limit is reduced to 12


Posted on Jan 17, 2012
Bn880
All for loops whether spawn or execVM or call are blocking and atomic in execution. They will not terminate or be interrupted after a certain time nor after a number of set iterations (unless there is a float overflow or other unexpected failure of the conditional statement). This differs from while loops, and has the potential to freeze up VBS2 and VBS2 NG.
Blocking for loop example: ok = [] spawn { j = 0; for "p" from 0 to 99999 do { j= j + 1; }; } result is j = 100000 and p is undefined.
Furthermore the VARNAME section of the for loop in the above example is not part of the global execution scope, meaning that 'p' global variable in the above example will be undefined once the for loop terminates. Local variables from the parent script scope can not be used and manipulated there. This differs from the for statements where the BEGIN statement variable will is part of global variable space and can inherited local variables from parent script scope.



Posted on Apr 29, 2010
Alef


for "_i" from 0 to 0  do{}; //will do once, with _i=0
for "_i" from 0 to -1 do{}; //will not do