{"name":"for","operator":null,"title":"for","type":"command","url":"https://community.bistudio.com/wiki/for","revid":353544,"updated":"2024-03-19T12:01:33Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"arma1","name":"Armed Assault","version":"1.00"},{"game":"arma2","name":"Arma 2","version":"1.00"},{"game":"arma2oa","name":"Arma 2: Operation Arrowhead","version":"1.50"},{"game":"tkoh","name":"Take On Helicopters","version":"1.00"},{"game":"arma3","name":"Arma 3","version":"0.50"}],"since":{"game":"arma1","name":"Armed Assault","version":"1.00"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"This operator creates a For Type which is used in the for-constructs.\n\n> [!WARNING]\n> 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.\n>\n> It is only kept for backward compatibility; always use the first syntax `for \"_i\" from 0 to _end do _code`.","syntaxes":[{"syntax":"for arguments","since":null,"effect":null,"exec":null,"params":[{"name":"arguments","type":"String","optional":false,"default":null,"description":"declares the index variable name like \"_myVar\". from and to are required, step is optional.","types":["String"],"since":null,"text":"arguments: String - declares the index variable name like \"_myVar\". from and to are required, step is optional."}],"returns":{"type":"For Type","types":["For Type"],"description":null,"text":"For Type"},"form":"unary","operator":null,"left":null,"right":{"text":"arguments","types":["String"]}},{"syntax":"for [init, condition, codeToExecute]","since":null,"effect":null,"exec":null,"params":[{"name":"init","type":"Code","optional":false,"default":null,"description":"loop variable definition","types":["Code"],"since":null,"text":"init: Code - loop variable definition"},{"name":"condition","type":"Code","optional":false,"default":null,"description":"called code returns false, leave the for-loop","types":["Code"],"since":null,"text":"condition: Code - called code returns false, leave the for-loop"},{"name":"codeToExecute","type":"Code","optional":false,"default":null,"description":"code to be run on each loop","types":["Code"],"since":null,"text":"codeToExecute: Code - code to be run on each loop"}],"returns":{"type":"For Type","types":["For Type"],"description":null,"text":"For Type"},"form":"unary","operator":null,"left":null,"right":{"text":"[init, condition, codeToExecute]","types":["Array"]}}],"examples":[{"text":"```sqf\n// will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive)\nfor \"_i\" from 1 to 10 do { systemChat str _i; };\n```","code":["// will output 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (the to value being inclusive)\nfor \"_i\" from 1 to 10 do { systemChat str _i; };"]},{"text":"```sqf\n// will output 9, 7, 5, 3, 1\nfor \"_i\" from 9 to 1 step -2 do { systemChat str _i; };\n```","code":["// will output 9, 7, 5, 3, 1\nfor \"_i\" from 9 to 1 step -2 do { systemChat str _i; };"]},{"text":"```sqf\n// will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\nfor [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] do { systemChat str _i };\n```","code":["// will output 0, 1, 2, 3, 4, 5, 6, 7, 8, 9\nfor [{ _i = 0 }, { _i < 10 }, { _i = _i + 1 }] do { systemChat str _i };"]},{"text":"```sqf\n// BAD CODE\n_i = 100;\nfor [{ _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };\nhint str _i; // 5\n\n// GOOD CODE (private keyword is recommended)\n_i = 100;\nfor [{ private _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };\nhint str _i; // 100\n\n// BEST CODE (primary syntax, fastest)\n_i = 100;\nfor \"_i\" from 0 to 4 do { /* code */ };\nhint str _i; // 100\n```","code":["// BAD CODE\n_i = 100;\nfor [{ _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };\nhint str _i; // 5\n\n// GOOD CODE (private keyword is recommended)\n_i = 100;\nfor [{ private _i = 0 }, { _i < 5 }, { _i = _i + 1 }] do { /* code */ };\nhint str _i; // 100\n\n// BEST CODE (primary syntax, fastest)\n_i = 100;\nfor \"_i\" from 0 to 4 do { /* code */ };\nhint str _i; // 100"]}],"multiplayer":null,"problems":"> [!NOTE]\n> **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:\n>\n> ```sqf\n> for \"_i\" from 0 to 2 do { hintSilent str _i; }; // works\n> for \"_I\" from 0 to 2 do { hintSilent str _I; }; // \"variable _i is not defined\" error\n> ```","aliases":[],"see_also":["Control Structures","while","do"],"notes":[{"user":"Alef","date":"2010-04-29T16:06:00Z","text":"```sqf\nfor \"_i\" from 0 to  0 do {}; // will do once, with _i = 0\nfor \"_i\" from 0 to -1 do {}; // will not do\n```"},{"user":"ffur2007slx2_5","date":"2014-04-15T12:54:00Z","text":"- Arma 3 v1.16 Please note the difference between the two syntaxes; `for *arguments*` detects Boolean in each scope while `for *array*` doesn’t. e.g.\n\n| command | Structure | Summary |\n|---|---|---|\n| for *array* | ```sqf<br>a = 0; b = true;<br>for [{ _i = 0 }, { _i < 10 && b }, { _i = _i + 1 }] do<br>{<br>    a = a + 1;<br>    if (a >= 7) then {b = false}<br>};<br>``` | loop can be exited via Boolean control, possible workaround can be e.g BIS_fnc_areEqual |\n| for *arguments* | ```sqf<br>a = 0;<br>for \"_i\" from 0 to 10 do<br>{<br>    a = a + 1;<br>    if (a >= 7) exitwith {};<br>};<br>``` | has to be exited via exitWith |\n\n- Never try to tell a decimal number via binary number in a loop; otherwise the loop will be infinite:\n\n```sqf\nfor [{_a = 0},{_a != 1},{_a = _a + 0.1}] do {}; // an infinite loop; _a will never be 1 so the scope will always be true\n```\n\nAny 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.\n- Avoid too large factorial multiply which may loose the leading indicator in result. And 12 is the biggest accessable factor in this example.\n\n```sqf\nfor [{_a = 2; _b = 1;},{_a < 100},{_a = _a + 1}] do {_b = _b * _a}; // _b =  1.#INF\n```"},{"user":"Killzone_Kid","date":"2015-06-04T19:27:00Z","text":"Variable name doesn't have to start with _. could be:\n\n```sqf\nfor \"LAlala\" from 0 to 0 do { hint str LAlala }; // 0\n```\n\nThe variable *LAlala* will exist only inside do {} scope and will not overwrite any variable of the same name that existed before."},{"user":"Nickorr","date":"2015-06-04T19:45:00Z","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 `for *array*` instead.\n\nExample, that won't work correctly:\n\n```sqf\n_xy = [1,2,3,4,5,6,7,8,9,10];\nfor \"_i\" from 0 to (count _xy - 1) do\n{\n    if ( _xy select _i == 3 ) then\n    {\n        _xy deleteAt _i;\n        _i = _i - 1;\n    };\n};\n```\n\n(Here the  step will still be checked by the cycle, which will lead to \"out of the array range\" error.)\n\nThis code will work correctly:\n\n```sqf\n_xy = [1,2,3,4,5,6,7,8,9,10];\nfor [{_i = 1},{_i<=(count _xy - 1)},{_i=_i+1}] do\n{\n    if ( _xy select _i == 3 ) then\n    {\n        _xy deleteAt _i;\n        _i = _i - 1;\n    };\n};\n```\n\n(The last step here will be `_i = 8` with array looking like this: `[1,2,4,5,6,7,8,9,10]`)"},{"user":"DreadedEntity","date":"2016-01-29T05:18:00Z","text":"For loops can be safely nested. This means that there should not be any problems with recursion.\n\n```sqf\n_array = [];\nfor \"_i\" from 0 to 3 do\n{\n    for \"_i\" from 0 to 3 do\n    {\n        _array pushBack _i;\n    };\n    _array pushBack _i;\n};\nhint str _array;\n```"}],"categories":["ArmA: Armed Assault: New Scripting Commands","ArmA: Armed Assault: Scripting Commands","Arma 2: Operation Arrowhead: Scripting Commands","Arma 2: Scripting Commands","Arma 3: Scripting Commands","Command Group: Program Flow","Introduced with Armed Assault version 1.00","Scripting Commands","Take On Helicopters: Scripting Commands"]}