for: Difference between revisions
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (No need to sort the table) |
Killzone Kid (talk | contribs) (It is up to end user to decide what syntax to use, definitely not deprecated) |
||
Line 9: | Line 9: | ||
| This operator creates a [[For Type]] which is used in the for-constructs. | | This operator creates a [[For Type]] which is used in the for-constructs. | ||
{{Important|Alternative syntax {{Inline code|[[for]] ''array'' [[do]] ''code''}} is [[Code_Optimisation#for|slower]] | {{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= | ||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
Line 30: | Line 30: | ||
|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> |Example 2= | ||
|x3= <code>[[for]] [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] [[do]] { [[systemChat]] [[str]] _i }; | |x3= <code>[[for]] [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] [[do]] { [[systemChat]] [[str]] _i }; </code> |Example 3= | ||
|x4 = <code>{{codecomment|// BAD CODE}} | |x4 = <code>{{codecomment|// BAD CODE}} |
Revision as of 08:35, 7 February 2019
Description
- Description:
- This operator creates a For Type which is used in the for-constructs.
- Groups:
- Uncategorised
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 array
- Parameters:
- array: Array - a 3-dimensional array of Code as follow: [init, condition, codeToExecute]
- Return Value:
- For Type
Examples
- Example 1:
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
Notes
- 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
-
- 1.16 Please note the difference between the two syntaxed;
for arguments
detects Boolean in each scope whilefor 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 like 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
- 1.16 Please note the difference between the two syntaxed;
- 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;