{"name":"for","title":"for","type":"command","url":"https://community.bistudio.com/wiki/for","revid":353544,"updated":"2024-03-19T12:01:33Z","groups":["Program Flow"],"status":"ok","games":{"arma1":"1.00","arma2":"1.00","arma2oa":"1.50","tkoh":"1.00","arma3":"0.50"},"since":"arma1 1.00","arma3_since":"0.50","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> It is only kept for backward compatibility; always use the first syntax `for \"_i\" from 0 to _end do _code`.","syntaxes":[{"syntax":"for arguments","params":[{"name":"arguments","type":"String","description":"declares the index variable name like \"_myVar\". from and to are required, step is optional.","type_expr":"String"}],"returns":{"type":"For Type","type_expr":"For Type"},"form":"unary","right":{"text":"arguments"}},{"syntax":"for [init, condition, codeToExecute]","params":[{"name":"init","type":"Code","description":"loop variable definition","type_expr":"Code"},{"name":"condition","type":"Code","description":"called code returns false, leave the for-loop","type_expr":"Code"},{"name":"codeToExecute","type":"Code","description":"code to be run on each loop","type_expr":"Code"}],"returns":{"type":"For Type","type_expr":"For Type"},"form":"unary","right":{"text":"[init, condition, codeToExecute]"}}],"examples":[{"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; };"]},{"code":["// will output 9, 7, 5, 3, 1\nfor \"_i\" from 9 to 1 step -2 do { systemChat str _i; };"]},{"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 };"]},{"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"]}],"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> ```","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\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 `_i = 9` step will still be checked by the cycle, which will lead to \"out of the array range\" error.)\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```"}]}