{"name":"exitWith","title":"exitWith","type":"command","url":"https://community.bistudio.com/wiki/exitWith","revid":379418,"updated":"2026-10-31T00:03:53Z","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":"Exits the **current** code scope. Often used for exiting do, for, count, forEach or the whole script.\n\n> [!NOTE]\n> See Variables - Local Variables Scope for more information.","syntaxes":[{"syntax":"ifType exitWith code","params":[{"name":"ifType","type":"If Type","type_expr":"If Type"},{"name":"code","type":"Code","type_expr":"Code"}],"returns":{"type":"Anything","type_expr":"Anything"},"form":"binary","left":{"text":"ifType"},"right":{"text":"code"}}],"examples":[{"text":"```sqf\nsystemChat \"start\";\nprivate _condition = true;\n\nif (_condition) then\n{\n    if (true) exitWith\n    {\n        systemChat \"exiting if _condition scope\";\n    };\n    systemChat \"never shown\";\n};\n\nsystemChat \"exiting #1 worked\";\n\nif (true) exitWith\n{\n    systemChat \"exiting the main scope = leaving the whole script\";\n};\n\nsystemChat \"never shown - the script has already ended\";\n```","code":["systemChat \"start\";\nprivate _condition = true;\n\nif (_condition) then\n{\n\tif (true) exitWith\n\t{\n\t\tsystemChat \"exiting if _condition scope\";\n\t};\n\tsystemChat \"never shown\";\n};\n\nsystemChat \"exiting #1 worked\";\n\nif (true) exitWith\n{\n\tsystemChat \"exiting the main scope = leaving the whole script\";\n};\n\nsystemChat \"never shown - the script has already ended\";"]},{"text":"```sqf\nfor \"_j\" from 1 to 10 do\n{\n    systemChat format [\"%1\", _j];\n\n    // the for loop will cease and code execution will continue after the end of the loop\n    if (_j == 5) exitWith\n    {\n        systemChat \"5 is enough\";\n    };\n};\nsystemChat \"Complete\";\n```","code":["for \"_j\" from 1 to 10 do\n{\n\tsystemChat format [\"%1\", _j];\n\n\t// the for loop will cease and code execution will continue after the end of the loop\n\tif (_j == 5) exitWith\n\t{\n\t\tsystemChat \"5 is enough\";\n\t};\n};\nsystemChat \"Complete\";"]},{"text":"Most loops will also terminate when their scope is exited. To exit and terminate scopes which are called every frame such as onEachFrame and waitUntil use the following examples:\n\n```sqf\nonEachFrame {\n    if (!alive player) exitWith\n    {\n        onEachFrame {};\n    };\n};\n```\n\n```sqf\n_time = time + 10;\nwaitUntil\n{\n    if (time > _time) exitWith { true };\n    false\n};\n```","code":["onEachFrame {\n\tif (!alive player) exitWith\n\t{\n\t\tonEachFrame {};\n\t};\n};","_time = time + 10;\nwaitUntil \n{\n\tif (time > _time) exitWith { true };\n\tfalse\n};"]},{"text":"```sqf\nwhile { true } do\n{\n    if (alive player) then\n    {\n        if (time > 300) exitWith // [wrong] - it will only leave the \"if alive player\" scope, remaining in the \"while true\" loop forever\n        {\n            hint \"exiting\";\n        };\n    };\n};\n```","code":["while { true } do\n{\n\tif (alive player) then\n\t{\n\t\tif (time > 300) exitWith // [wrong] - it will only leave the \"if alive player\" scope, remaining in the \"while true\" loop forever\n\t\t{\n\t\t\thint \"exiting\";\n\t\t};\n\t};\n};"]},{"text":"```sqf\nif (_condition) exitWith\n{\n    hint \"reached\";\n};\n// else { hint \"not reached\" }; // [wrong] - using else does not work and makes no sense here\n```\n\n```sqf\nif (_condition) exitWith\n{\n    hint \"reached\";\n};\nhint \"not reached\"; // [correct] - if _condition is met, the scope has already been exited by now\n```","code":["if (_condition) exitWith\n{\n\thint \"reached\";\n};\n// else { hint \"not reached\" }; // [wrong] - using else does not work and makes no sense here","if (_condition) exitWith\n{\n\thint \"reached\";\n};\nhint \"not reached\"; // [correct] - if _condition is met, the scope has already been exited by now"]}],"see_also":["scopeName","breakOut","breakTo","else","Control Structures","then","assert","try","catch","throw"],"notes":[{"user":"RoehrenRadio","date":"2010-05-28T22:15:00Z","text":"Since Arma 2 uses Blocks in FSM as any ordinary Handle like while, for etc. in Scripts, **ExitWith** also only closes the Block in the FSM."},{"user":"Killzone_Kid","date":"2013-08-04T12:20:00Z","text":"The command will exit the current scope - no ifs no buts. If the current scope is a loop, it will exit the loop. If the current scope is the main body of a script, it will exit the script. For more understanding of scopes and exitWith have a look at [this resource](http://killzonekid.com/arma-scripting-tutorials-scopes/)."},{"user":"Killzone_Kid","date":"2016-03-10T08:31:00Z","text":"exitWith cannot be used in event handlers with override ability to simply exit with override value. The following is incorrect:\n\n```sqf\n// INCORRECT USAGE\nonMapSingleClick { if (!isServer) exitWith { true } };\n```\n\nThe override value must be returned in the main scope of EH, but since it is exited with exitWith, it never happens. The correct way in this case would be:\n\n```sqf\n// CORRECT USAGE\nonMapSingleClick { call { if (!isServer) exitWith { true } } };\n```\n\nexitWith will exit current call scope only and override value therefore will appear in the main scope of the EH, right where we want it."}]}