{"name":"while","operator":null,"title":"while","type":"command","url":"https://community.bistudio.com/wiki/while","revid":369060,"updated":"2024-09-02T20:24:55Z","groups":["Program Flow"],"status":"ok","replaced_by":[],"games":[{"game":"ofp","name":"Operation Flashpoint","version":"1.85"},{"game":"ofpe","name":"Operation Flashpoint: Elite","version":"1.00"},{"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":"ofp","name":"Operation Flashpoint","version":"1.85"},"arma3_since":"0.50","branch":null,"locality":{"argument":null,"effect":null,"server_exec":false},"exec":null,"description":"Repeats Code while the given condition is true. A part of while-do construct.\n\n> [!IMPORTANT]\n> - A while loop does not have any suspension, meaning that if used in scheduled environment without any suspension (sleep or uiSleep) the code will run multiple times per frame and consumes the 3 ms per frame scheduler execution limit. This should be avoided if not intended (see #Example 3).\n> - In non-scheduled environment, while do loop is limited to 10,000 iterations, after which it exits even if condition is still true.","syntaxes":[{"syntax":"while condition","since":null,"effect":null,"exec":null,"params":[{"name":"condition","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"condition: Code"}],"returns":{"type":"While Type","types":["While Type"],"description":null,"text":"While Type"},"form":"unary","operator":null,"left":null,"right":{"text":"condition","types":["Code"]}}],"examples":[{"text":"```sqf\nwhile { a < b } do { a = a + 1 };\n```","code":["while { a < b } do { a = a + 1 };"]},{"text":"A practical example: Repair all members of a group to such a level that they are able to stand up:\n\n```sqf\n{\n    if (alive _x) then\n    {\n        while { not canStand _x } do\n        {\n            _x setDamage (damage _x - 0.01);\n        };\n    };\n} forEach units group unitname;\n```","code":["{\n\tif (alive _x) then\n\t{\n\t\twhile { not canStand _x } do\n\t\t{\n\t\t\t_x setDamage (damage _x - 0.01);\n\t\t};\n\t};\n} forEach units group unitname;"]},{"text":"```sqf\n0 spawn {\n    // warning: while loop without suspension executes multiple times per frame\n    private _counter = 0;\n    private _endTime = diag_tickTime + 5;\n    private _frameNo = diag_frameNo;\n    while { diag_tickTime < _endTime } do\n    {\n        _counter = _counter + 1;\n    };\n    // in an empty mission, the _counter may go well over 2000 times per frame!\n    hint format [\"Average Execution: %1 times per frame\", _counter / (diag_frameNo - _frameNo)];\n\n    // with suspension\n    private _counter = 0;\n    private _endTime = diag_tickTime + 5;\n    private _frameNo = diag_frameNo;\n    while { diag_tickTime < _endTime } do\n    {\n        _counter = _counter + 1;\n        uiSleep 0.001; // waits at least 1 frame\n    };\n    // _counter says one per frame, as expected\n    hint format [\"Average Execution: %1 times per frame\", _counter / (diag_frameNo - _frameNo)];\n};\n```","code":["0 spawn {\n\t// warning: while loop without suspension executes multiple times per frame\n\tprivate _counter = 0;\n\tprivate _endTime = diag_tickTime + 5;\n\tprivate _frameNo = diag_frameNo;\n\twhile { diag_tickTime < _endTime } do\n\t{\n\t\t_counter = _counter + 1;\n\t};\n\t// in an empty mission, the _counter may go well over 2000 times per frame!\n\thint format [\"Average Execution: %1 times per frame\", _counter / (diag_frameNo - _frameNo)];\n\n\t// with suspension\n\tprivate _counter = 0;\n\tprivate _endTime = diag_tickTime + 5;\n\tprivate _frameNo = diag_frameNo;\n\twhile { diag_tickTime < _endTime } do\n\t{\n\t\t_counter = _counter + 1;\n\t\tuiSleep 0.001; // waits at least 1 frame\n\t};\n\t// _counter says one per frame, as expected\n\thint format [\"Average Execution: %1 times per frame\", _counter / (diag_frameNo - _frameNo)];\n};"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["Control Structures","waitUntil","for","do"],"notes":[{"user":"Kronzky","date":"2008-05-14T08:40:00Z","text":"The boolean code that is used to evaluate the while condition can be preceded by code that executes a regular command.\n\n```sqf\nwhile { _a =_a + 1; _a < 10 } do { /* ... */ };\n```"}],"categories":["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 Operation Flashpoint version 1.85","Operation Flashpoint: Elite: Scripting Commands","Operation Flashpoint: New Scripting Commands","Operation Flashpoint: Scripting Commands","Scripting Commands","Take On Helicopters: Scripting Commands"]}