for
(Redirected from for var)
Jump to navigation
Jump to search
Description
- Description:
- This operator creates a For Type which is used in the for-constructs.
- Problems:
- 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:
- Example 3:
- 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
- Posted on Apr 29, 2010 - 16:06 (UTC)
- Posted on Apr 15, 2014 - 12:54 (UTC)
-
- 1.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 loop can be exited via Boolean control, possible workaround can be e.g BIS_fnc_areEqual for arguments 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:
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.
- Posted on Jun 04, 2015 - 19:27 (UTC)
- Variable name doesn't have to start with _. could be: The variable LAlala will exist only inside do {} scope and will not overwrite any variable of the same name that existed before.
- 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:(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: (The last step here will be _i = 8 with array looking like this: [1,2,4,5,6,7,8,9,10])
- 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.
Categories:
- Scripting Commands
- Introduced with Armed Assault version 1.00
- ArmA: Armed Assault: New Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Program Flow