for: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
(for is an independent operator)
m (Text replacement - "{{arma2}}" to "{{GameCategory|arma2|link= y}}")
 
(46 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma |= Game name
|game1= arma1
|version1= 1.00


|1.00|= Game version
|game2= arma2
____________________________________________________________________________________________
|version2= 1.00


| This operator creates a [[For Type]] which is used in the for-constructs (There is the [[for forspec]] and the [[for var]] syntax available for this construct) |= Description
|game3= arma2oa
____________________________________________________________________________________________
|version3= 1.50


| '''for''' arg |= Syntax
|game4= tkoh
|version4= 1.00


|p1= arg: [[String]] or [[Array]] - When using String it declares the index variable name like "_myVar" and when using Array it must be a 3-dimensional Array of [[Code]]|= Parameter 1
|game5= arma3
|version5= 0.50


| [[For Type]] |= Return value
|gr1= Program Flow
____________________________________________________________________________________________


|x1= <code>[[for]] "_i" [[from]] 1 [[to]] 10 [[do]] {[[debugLog]] _i;};</code> |= Example 1
|descr= This operator creates a [[For Type]] which is used in the for-constructs.
|x2= <code>[[for]] "_i" [[from]] 9 [[to]] 1 [[step]] -2 [[do]] {[[debugLog]] _i;};</code> |= Example 2
{{Feature|warning|
|x3= <code> [[for]] [{_i=0}, {_i < 10}, {_i = _i + 1}] [[do]] {hint str _i};</code> |=Example 3
The alternative syntax <sqf inline>for _array do _code</sqf> is [[Code Optimisation#for|slower]] than the first syntax because the condition is checked in every iteration, and it has a few bugs.<br>
____________________________________________________________________________________________
It is only kept for backward compatibility; always use the first syntax <sqf inline>for "_i" from 0 to _end do _code</sqf>.
}}
 
|pr= {{Feature|arma2|There is an {{GameCategory|arma2|link= y}} issue (not present in {{Name|arma2oa|short}} nor in {{arma3}}) where having a capital letter in the variable name '''will''' throw an error:
<sqf>
for "_i" from 0 to 2 do { hintSilent str _i; }; // works
for "_I" from 0 to 2 do { hintSilent str _I; }; // "variable _i is not defined" error
</sqf>
}}


| [[for forspec|for [ ]]], [[for_do|for do]], [[Control Structures]] |= See also
|s1= [[for]] arguments


|p1= arguments: [[String]] - declares the index variable name like "_myVar". [[from]] and [[to]] are required, [[step]] is optional.
|r1= [[For Type]]
|s2= [[for]] [init, condition, codeToExecute]
|p21= init: [[Code]] - loop variable definition
|p22= condition: [[Code]] - [[call]]ed code returns [[false]], leave the [[for]]-loop
|p23= codeToExecute: [[Code]] - code to be run on each loop
|r2= [[For Type]]
|x1= <sqf>
// will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive)
for "_i" from 1 to 10 do { systemChat str _i; };
</sqf>
|x2= <sqf>
// will output 9, 7, 5, 3, 1
for "_i" from 9 to 1 step -2 do { systemChat str _i; };
</sqf>
|x3= <sqf>
// will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
for [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] do { systemChat str _i };
</sqf>
|x4= <sqf>
// BAD CODE
_i = 100;
for [{ _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };
hint str _i; // 5
// GOOD CODE (private keyword is recommended)
_i = 100;
for [{ private _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };
hint str _i; // 100
// BEST CODE (primary syntax, fastest)
_i = 100;
for "_i" from 0 to 4 do { /* code */ };
hint str _i; // 100
</sqf>
|seealso= [[Control Structures]] [[while]] [[do]]
}}
{{Note
|user= Alef
|timestamp= 20100429160600
|text= <sqf>
for "_i" from 0 to  0 do {}; // will do once, with _i = 0
for "_i" from 0 to -1 do {}; // will not do
</sqf>
}}
{{Note
|user= ffur2007slx2_5
|timestamp= 20140415125400
|text= <nowiki/>
* {{GVI|arma 3|1.16}} Please note the difference between the two syntaxes; {{hl|[[for]] ''arguments''}} detects Boolean in each scope while {{hl|[[for]] ''array''}} doesn’t. e.g.
{{{!}} class="wikitable"
! command
! Structure
! Summary
{{!}}-
{{!}} [[for]]&nbsp;''array''
{{!}}
<sqf>
a = 0; b = true;
for [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] do
{
a = a + 1;
if (a >= 7) then {b = false}
};
</sqf>
{{!}} loop can be exited via [[Boolean]] control, possible workaround can be e.g [[BIS_fnc_areEqual]]
{{!}}-
{{!}} [[for]]&nbsp;''arguments''
{{!}}
<sqf>
a = 0;
for "_i" from 0 to 10 do
{
a = a + 1;
if (a >= 7) exitwith {};
};
</sqf>
{{!}} has to be exited via [[exitWith]]
{{!}}}
* Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:
<sqf>for [{_a = 0},{_a != 1},{_a = _a + 0.1}] do {}; // an infinite loop; _a will never be 1 so the scope will always be true</sqf>
Any binary number behind the decimal point is always the sum of 1/2, 1/4, 1/8, 1/16 etc. so decimal number with odd denominator like 1/3 or 1/10 cannot be exactly equal to each other.
* Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.
<sqf>for [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] do {_b = _b * _a}; // _b =  1.#INF</sqf>
}}
{{Note
|user= Killzone_Kid
|timestamp= 20150604192700
|text= Variable name doesn't have to start with _. could be:
<sqf>for "LAlala" from 0 to 0 do { hint str LAlala }; // 0</sqf>
The variable ''LAlala'' will exist only inside [[do]] {} scope and will not overwrite any variable of the same name that existed before.
}}
{{Note
|user= Nickorr
|timestamp= 20150604194500
|text= Dont use this notation if you plan to change the cycle ranges dynamically. The range values are checked only before the cycle started. Use {{hl|[[for]] ''array''}} instead. <br>
Example, that won't work correctly:
<sqf>
_xy = [1,2,3,4,5,6,7,8,9,10];
for "_i" from 0 to (count _xy - 1) do
{
if ( _xy select _i == 3 ) then
{
_xy deleteAt _i;
_i = _i - 1;
};
};
</sqf>
(Here the {{hl|c= <nowiki>_i = 9</nowiki>}} step will still be checked by the cycle, which will lead to "out of the array range" error.) <br>
This code will work correctly:
<sqf>
_xy = [1,2,3,4,5,6,7,8,9,10];
for [{_i = 1},{_i<=(count _xy - 1)},{_i=_i+1}] do
{
if ( _xy select _i == 3 ) then
{
_xy deleteAt _i;
_i = _i - 1;
};
};
</sqf>
(The last step here will be {{hl|_i {{=}} 8}} with array looking like this: {{hl|[1,2,4,5,6,7,8,9,10]}})
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= DreadedEntity
|timestamp= 20160129051800
|text= For loops can be safely nested. This means that there should not be any problems with recursion.
<sqf>
_array = [];
for "_i" from 0 to 3 do
{
for "_i" from 0 to 3 do
{
_array pushBack _i;
};
_array pushBack _i;
};
hint str _array;
</sqf>
}}

Latest revision as of 13:01, 19 March 2024

Hover & click on the images for description

Description

Description:
This operator creates a For Type which is used in the for-constructs.
The alternative syntax for _array do _code is slower than the first syntax because the condition is checked in every iteration, and it has a few bugs.
It is only kept for backward compatibility; always use the first syntax for "_i" from 0 to _end do _code.
Problems:
Arma 2
There is an Arma 2 issue (not present in Arma 2:OA nor in Arma 3) where having a capital letter in the variable name will throw an error:
for "_i" from 0 to 2 do { hintSilent str _i; }; // works for "_I" from 0 to 2 do { hintSilent str _I; }; // "variable _i is not defined" error
Groups:
Program Flow

Syntax

Syntax:
for arguments
Parameters:
arguments: String - declares the index variable name like "_myVar". from and to are required, step is optional.
Return Value:
For Type

Alternative Syntax

Syntax:
for [init, condition, codeToExecute]
Parameters:
init: Code - loop variable definition
condition: Code - called code returns false, leave the for-loop
codeToExecute: Code - code to be run on each loop
Return Value:
For Type

Examples

Example 1:
// will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive) for "_i" from 1 to 10 do { systemChat str _i; };
Example 2:
// will output 9, 7, 5, 3, 1 for "_i" from 9 to 1 step -2 do { systemChat str _i; };
Example 3:
// will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 for [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] do { systemChat str _i };
Example 4:
// BAD CODE _i = 100; for [{ _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ }; hint str _i; // 5 // GOOD CODE (private keyword is recommended) _i = 100; for [{ private _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ }; hint str _i; // 100 // BEST CODE (primary syntax, fastest) _i = 100; for "_i" from 0 to 4 do { /* code */ }; hint str _i; // 100

Additional Information

See also:
Control Structures while do

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Alef - c
Posted on Apr 29, 2010 - 16:06 (UTC)
for "_i" from 0 to 0 do {}; // will do once, with _i = 0 for "_i" from 0 to -1 do {}; // will not do
ffur2007slx2_5 - c
Posted on Apr 15, 2014 - 12:54 (UTC)
  • Arma 3 logo black.png1.16 Please note the difference between the two syntaxes; for arguments detects Boolean in each scope while for array doesn’t. e.g.
command Structure Summary
for array

a = 0; b = true; for [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] do { a = a + 1; if (a >= 7) then {b = false} };

loop can be exited via Boolean control, possible workaround can be e.g BIS_fnc_areEqual
for arguments

a = 0; for "_i" from 0 to 10 do { a = a + 1; if (a >= 7) exitwith {}; };

has to be exited via exitWith
  • Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:

for [{_a = 0},{_a != 1},{_a = _a + 0.1}] do {}; // an infinite loop; _a will never be 1 so the scope will always be true
Any binary number behind the decimal point is always the sum of 1/2, 1/4, 1/8, 1/16 etc. so decimal number with odd denominator like 1/3 or 1/10 cannot be exactly equal to each other.

  • Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.

for [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] do {_b = _b * _a}; // _b = 1.#INF

Killzone_Kid - c
Posted on Jun 04, 2015 - 19:27 (UTC)
Variable name doesn't have to start with _. could be:
for "LAlala" from 0 to 0 do { hint str LAlala }; // 0
The variable LAlala will exist only inside do {} scope and will not overwrite any variable of the same name that existed before.
Nickorr - c
Posted on Jun 04, 2015 - 19:45 (UTC)
Dont use this notation if you plan to change the cycle ranges dynamically. The range values are checked only before the cycle started. Use for array instead.
Example, that won't work correctly:
_xy = [1,2,3,4,5,6,7,8,9,10]; for "_i" from 0 to (count _xy - 1) do { if ( _xy select _i == 3 ) then { _xy deleteAt _i; _i = _i - 1; }; };
(Here the _i = 9 step will still be checked by the cycle, which will lead to "out of the array range" error.)
This code will work correctly:
_xy = [1,2,3,4,5,6,7,8,9,10]; for [{_i = 1},{_i<=(count _xy - 1)},{_i=_i+1}] do { if ( _xy select _i == 3 ) then { _xy deleteAt _i; _i = _i - 1; }; };
(The last step here will be _i = 8 with array looking like this: [1,2,4,5,6,7,8,9,10])
DreadedEntity - c
Posted on Jan 29, 2016 - 05:18 (UTC)
For loops can be safely nested. This means that there should not be any problems with recursion.
_array = []; for "_i" from 0 to 3 do { for "_i" from 0 to 3 do { _array pushBack _i; }; _array pushBack _i; }; hint str _array;