{"name":"exitWith","operator":null,"title":"exitWith","type":"command","url":"https://community.bistudio.com/wiki/exitWith","revid":369326,"updated":"2024-09-03T18:42:59Z","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":"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","since":null,"effect":null,"exec":null,"params":[{"name":"ifType","type":"If Type","optional":false,"default":null,"description":null,"types":["If Type"],"since":null,"text":"ifType: If Type"},{"name":"code","type":"Code","optional":false,"default":null,"description":null,"types":["Code"],"since":null,"text":"code: Code"}],"returns":{"type":"Anything","types":["Anything"],"description":null,"text":"Anything"},"form":"binary","operator":null,"left":{"text":"ifType","types":["If Type"]},"right":{"text":"code","types":["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"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["scopeName","breakOut","breakTo","else","Control Structures","then","assert","try","catch","throw"],"notes":[],"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"]}