for: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\[\[Category:[ _]?Scripting[ _]Commands[ _]Arma[ _]2(\|.*)\]\]" to "{{GameCategory|arma2|Scripting Commands}}")
m (Text replacement - ";[ ]+ " to "; ")
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


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


|gr1= Program Flow |GROUP1=
|game2= arma2
|version2= 1.00


|game3= arma2oa
|version3= 1.50


|game4= tkoh
|version4= 1.00


|1.00|Game version=
|game5= arma3
____________________________________________________________________________________________
|version5= 0.50


| This operator creates a [[For Type]] which is used in the for-constructs.
|gr1= Program Flow
{{Important|Alternative syntax {{Inline code|[[for]] ''array'' [[do]] ''code''}} is [[Code_Optimisation#for|slower]] than the alternative because the condition is checked every iteration.}} |Description=
____________________________________________________________________________________________


| [[for]] arguments |Syntax=
|descr= This operator creates a [[For Type]] which is used in the for-constructs.
{{Feature|Warning|The alternative syntax {{ic|[[for]] ''array'' [[do]] ''code''}} is [[Code_Optimisation#for|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. You should always use the first syntax {{ic|[[for]] ''"_var"'' [[from]] ''start'' [[to]] ''end'' [[do]] ''code''}}.}}


|p1= arguments: [[String]] - declares the index variable name like "_myVar". [[from]] and [[to]] are required, [[step]] is optional. |Parameter 1=


| [[For Type]] |Return value=
|pr= {{Feature|arma2|There is an [[:Category:Arma 2|{{arma2}}]] issue (not present in {{Name|arma2oa|short}} nor in {{arma3}}) where having a capital letter in the variable name '''will''' throw an error:
____________________________________________________________________________________________
<code>[[for]] "_i" [[from]] 0 [[to]] 2 [[do]] { [[hintSilent]] [[str]] _i; }; {{cc|works}}<br><!--
-->[[for]] "_I" [[from]] 0 [[to]] 2 [[do]] { [[hintSilent]] [[str]] _I; }; {{cc|"variable _i is not defined" error}}</code>}}


|s2= [[for]] [init, condition, codeToExecute] |Syntax 2=
|s1= [[for]] arguments


|p21= init: [[Code]] - loop variable definition |Parameter 21=
|p1= arguments: [[String]] - declares the index variable name like "_myVar". [[from]] and [[to]] are required, [[step]] is optional.
|p22= condition: [[Code]] - [[call]]ed code returns [[false]], leave the [[for]]-loop |Parameter 22=
|p23= codeToExecute: [[Code]] - code to be run on each loop |Parameter 23=


|r2= [[For Type]] |Return value 2=
|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= <code>{{cc|will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive)}}
|x1= <code>{{cc|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; };</code> |Example 1=
[[for]] "_i" [[from]] 1 [[to]] 10 [[do]] { [[systemChat]] [[str]] _i; };</code>


|x2= <code>[[for]] "_i" [[from]] 9 [[to]] 1 [[step]] -2 [[do]] { [[systemChat]] [[str]] _i; };</code> |Example 2=
|x2= <code>[[for]] "_i" [[from]] 9 [[to]] 1 [[step]] -2 [[do]] { [[systemChat]] [[str]] _i; };</code>


|x3= <code>[[for]] [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] [[do]] { [[systemChat]] [[str]] _i };</code> |Example 3=
|x3= <code>[[for]] [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] [[do]] { [[systemChat]] [[str]] _i };</code>


|x4= <code>{{cc|BAD CODE}}
|x4= <code>{{cc|BAD CODE}}
_i = 100;  
_i = 100;
[[for]] [{_i = 0}, {_i < 5}, {_i = _i + 1}] [[do]] {};  
[[for]] [{_i = 0}, {_i < 5}, {_i = _i + 1}] [[do]] {};
[[hint]] [[str]] _i; {{cc|5}}
[[hint]] [[str]] _i; {{cc|5}}


{{cc|GOOD CODE ([[private]] keyword is recommended)}}
{{cc|GOOD CODE ([[private]] keyword is recommended)}}
_i = 100;  
_i = 100;
[[for]] [{[[private]] _i = 0}, {_i < 5}, {_i = _i + 1}] [[do]] {};  
[[for]] [{[[private]] _i = 0}, {_i < 5}, {_i = _i + 1}] [[do]] {};
[[hint]] [[str]] _i; {{cc|100}}</code> |Example 4=
[[hint]] [[str]] _i; {{cc|100}}</code>
____________________________________________________________________________________________


| [[Control Structures]], [[while]], [[do]] |See also=
|seealso= [[Control Structures]], [[while]], [[do]]
}}
}}


<h3 style="display:none">Notes</h3>
<dl class="command_description">
<dl class="command_description">
<!-- Note Section BEGIN -->


<dd class="notedate">Posted on 29 April 2010 (CEST)
<dt><dt>
<dt class="note">[[User:Alef|alef]]
<dd class="notedate">Posted on 29 April 2010 (CEST)</dd>
<dt class="note">[[User:Alef|alef]]</dt>
<dd class="note"><code>[[for]] "_i" [[from]] 0 [[to]]  0 [[do]] {}; {{cc|will do once, with _i {{=}} 0}}
<dd class="note"><code>[[for]] "_i" [[from]] 0 [[to]]  0 [[do]] {}; {{cc|will do once, with _i {{=}} 0}}
[[for]] "_i" [[from]] 0 [[to]] -1 [[do]] {}; {{cc|will not do}}
[[for]] "_i" [[from]] 0 [[to]] -1 [[do]] {}; {{cc|will not do}}
</code>
</code>
</dd>


<dd class="notedate">Posted on Apr 15, 2014 - 12:54
<dt><dt>
<dt class="note">[[User:ffur2007slx2_5|ffur2007slx2_5]]<dd class="note">
<dd class="notedate">Posted on Apr 15, 2014 - 12:54</dd>
<dt class="note">[[User:ffur2007slx2_5|ffur2007slx2_5]]</dt>
<dd class="note">


*{{GVI|arma 3|1.16}} Please note the difference between the two syntaxes; {{Inline code|[[for]] ''arguments''}} detects Boolean in each scope while {{Inline code|[[for]] ''array''}} doesn’t. e.g.
* {{GVI|arma 3|1.16}} Please note the difference between the two syntaxes; {{ic|[[for]] ''arguments''}} detects Boolean in each scope while {{ic|[[for]] ''array''}} doesn’t. e.g.
{| class="wikitable"
{| class="wikitable"
! command
! command
Line 75: Line 85:
  a = 0; b = [[true]];
  a = 0; b = [[true]];
  [[for]] [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] [[do]] {
  [[for]] [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] [[do]] {
  a = a + 1;  
  a = a + 1;
  [[if]] (a >= 7) [[then]] {b = [[false]]}
  [[if]] (a >= 7) [[then]] {b = [[false]]}
  }
  }
Line 90: Line 100:


|}
|}
<br>
* Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:
* Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:
<code>[[for]] [{_a = 0},{_a != 1},{_a = _a + 0.1}] [[do]] {}; {{cc|an infinite loop; _a will never be 1 so the scope will always be true}}</code>
<code>[[for]] [{_a = 0},{_a != 1},{_a = _a + 0.1}] [[do]] {}; {{cc|an infinite loop; _a will never be 1 so the scope will always be true}}</code>
Line 96: Line 105:
* Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.
* Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.
<code>[[for]] [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] [[do]] {_b = _b * _a}; {{cc|_b {{=}} 1.#INF}}</code>
<code>[[for]] [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] [[do]] {_b = _b * _a}; {{cc|_b {{=}} 1.#INF}}</code>
</dd>


<dt><dt>
<dd class="notedate">Posted on June 4, 2015 - 19:27 (UTC)</dd>
<dd class="notedate">Posted on June 4, 2015 - 19:27 (UTC)</dd>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
Line 104: Line 115:
</dd>
</dd>


<dt><dt>
<dd class="notedate">Posted on June 4, 2015 - 19:45 (UTC)</dd>
<dd class="notedate">Posted on June 4, 2015 - 19:45 (UTC)</dd>
<dt class="note">[[User:Nickorr|Nickorr]]</dt>
<dt class="note">[[User:Nickorr|Nickorr]]</dt>
<dd class="note">
<dd class="note">
Dont use this notation if you plan to change the cycle ranges dynamically. The range values are checked only before the cycle started. Use {{Inline code|[[for]] ''array''}} instead. <br>
Dont use this notation if you plan to change the cycle ranges dynamically. The range values are checked only before the cycle started. Use {{ic|[[for]] ''array''}} instead. <br>


Example, that won't work correctly:
Example, that won't work correctly:
Line 114: Line 126:
if ( _xy select _i == 3 ) then {
if ( _xy select _i == 3 ) then {
_xy deleteAt _i;
_xy deleteAt _i;
_i = _i - 1;
_i = _i - 1;
};
};
};</nowiki></code>
};</nowiki></code>
(Here the <tt><nowiki>_i = 9</nowiki></tt> step will still be checked by the cycle, which will lead to "out of the array range" error.) <br>
(Here the <tt><nowiki>_i = 9</nowiki></tt> step will still be checked by the cycle, which will lead to "out of the array range" error.) <br>
This code will work correctly:
This code will work correctly:
<code><nowiki>_xy = [1,2,3,4,5,6,7,8,9,10];
<code>_xy = [1,2,3,4,5,6,7,8,9,10];
for [{_i=1},{_i<=(count _xy - 1)},{_i=_i+1}] do {
for [{_i = 1},{_i<=(count _xy - 1)},{_i=_i+1}] do {
if ( _xy select _i == 3 ) then {
if ( _xy select _i == 3 ) then {
_xy deleteAt _i;
_xy deleteAt _i;
_i = _i - 1;
_i = _i - 1;
};
};
};</nowiki></code>
};</code>
(The last step here will be <tt><nowiki>_i = 8</nowiki></tt> with array looking like this: <tt><nowiki>[1,2,4,5,6,7,8,9,10]</nowiki></tt>)
(The last step here will be <tt><nowiki>_i = 8</nowiki></tt> with array looking like this: <tt><nowiki>[1,2,4,5,6,7,8,9,10]</nowiki></tt>)
</dd>
</dd>


<dt><dt>
<dd class="notedate">Posted on January 29, 2016 - 05:18 (UTC)</dd>
<dd class="notedate">Posted on January 29, 2016 - 05:18 (UTC)</dd>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
<dt class="note">[[User:DreadedEntity|DreadedEntity]]</dt>
Line 145: Line 158:
</dd>
</dd>


<dd class="notedate">Posted on June 3, 2020 - 11:45</dd>
<dt class="note">[[User:Lou Montana|Lou Montana]]</dt>
<dd class="note"> There is an [[{{arma2}}]] issue (not present in Arrowhead nor in {{arma3}}) where having a capital letter in the variable name '''will''' throw an error:
<code>[[for]] "_i" [[from]] 0 [[to]] 2 [[do]] { [[hintSilent]] [[str]] _i; }; {{cc|works}}
[[for]] "_I" [[from]] 0 [[to]] 2 [[do]] { [[hintSilent]] [[str]] _I; }; {{cc|"variable _i is not defined" error}}</code></dd>
<!-- Note Section END -->
</dl>
</dl>
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}

Revision as of 01:55, 8 August 2021

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. You should always use the first syntax for "_var" from start 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:
for "_i" from 9 to 1 step -2 do { systemChat str _i; };
Example 3:
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 {}; hint str _i; // 5 // GOOD CODE (private keyword is recommended) _i = 100; for [{private _i = 0}, {_i < 5}, {_i = _i + 1}] do {}; hint str _i; // 100

Additional Information

See also:
Control Structureswhiledo

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
Posted on 29 April 2010 (CEST)
alef
for "_i" from 0 to 0 do {}; // will do once, with _i = 0 for "_i" from 0 to -1 do {}; // will not do
Posted on Apr 15, 2014 - 12:54
ffur2007slx2_5
  • 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

Posted on June 4, 2015 - 19:27 (UTC)
Killzone Kid
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.
Posted on June 4, 2015 - 19:45 (UTC)
Nickorr
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])
Posted on January 29, 2016 - 05:18 (UTC)
DreadedEntity
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;