{"name":"switch","operator":null,"title":"switch","type":"command","url":"https://community.bistudio.com/wiki/switch","revid":352209,"updated":"2024-02-01T23:26:04Z","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":"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":"Checks if the given parameter matches any case. If so, the code block of that case will be executed. After that the switch ends so no further cases will be checked.\n\nIf a case has no code block the code of the next case will automatically be executed, making it possible to formulate a logical \"or\" for cases which would otherwise contain the exact same code (see #Example 2).\n\nThe default block will be executed **only** if no case matches, no matter its position inside the code block. It is not a case, and will **never** be entered by fallthrough.\n\n> [!NOTE]\n> - String comparison is case-sensitive. Use toUpper, toLower, toLowerANSI or toUpperANSI to force all strings to the same case.\n> - if no default block is provided and no case is matched, the switch block returns the default value true, otherwise it returns what the valid case block returns.","syntaxes":[{"syntax":"switch value","since":null,"effect":null,"exec":null,"params":[{"name":"value","type":"Anything","optional":false,"default":null,"description":null,"types":["Anything"],"since":null,"text":"value: Anything"}],"returns":{"type":"Switch Type","types":["Switch Type"],"description":null,"text":"Switch Type"},"form":"unary","operator":null,"left":null,"right":{"text":"value","types":["Anything"]}}],"examples":[{"text":"```sqf\nswitch (floor random 5) do\n{\n    case 1: { hint \"one\"; };\n    case 2: { hint \"two\"; };\n    default { hint \"zero, three or four\" };\n};\n```","code":["switch (floor random 5) do\n{\n\tcase 1: { hint \"one\"; };\n\tcase 2: { hint \"two\"; };\n\tdefault { hint \"zero, three or four\" };\n};"]},{"text":"```sqf\nswitch (_condition) do\n{\n    case \"string1\";\n    case \"string2\": { hint \"string1 or string2\" };\n    case \"string3\";\n    case \"string4\": { hint \"string3 or string4\" };\n    case \"string5\"; // will never enter default's code\n    default { hint \"default\" };\n};\n```","code":["switch (_condition) do\n{\n\tcase \"string1\";\n\tcase \"string2\": { hint \"string1 or string2\" };\n\tcase \"string3\";\n\tcase \"string4\": { hint \"string3 or string4\" };\n\tcase \"string5\"; // will never enter default's code\n\tdefault { hint \"default\" };\n};"]},{"text":"```sqf\nprivate _color = switch (side player) do\n{\n    case west: { \"ColorGreen\" };\n    case east: { \"ColorRed\" };\n};\n```","code":["private _color = switch (side player) do\n{\n\tcase west: { \"ColorGreen\" };\n\tcase east: { \"ColorRed\" };\n};"]},{"text":"```sqf\nprivate _fn_moveForward =    { /*...code...*/ };\nprivate _fn_moveBackward =    { /*...code...*/ };\nprivate _fn_invalidKey =    { /*...code...*/ };\n\nswitch true do\n{\n    case (_dikCode in actionKeys \"MoveForward\"):    _fn_moveForward;\n    case (_dikCode in actionKeys \"MoveBackward\"):    _fn_moveBackward;\n    default _fn_invalidKey;\n};\n```","code":["private _fn_moveForward =\t{ /*...code...*/ };\nprivate _fn_moveBackward =\t{ /*...code...*/ };\nprivate _fn_invalidKey =\t{ /*...code...*/ };\n\nswitch true do\n{\n\tcase (_dikCode in actionKeys \"MoveForward\"):\t_fn_moveForward;\n\tcase (_dikCode in actionKeys \"MoveBackward\"):\t_fn_moveBackward;\n\tdefault _fn_invalidKey;\n};"]},{"text":"```sqf\nswitch _var do\n{\n    case \"0\";\n    default { hint str [\"default\", _var] };\n    case \"3\": { hint str [\"3\", _var] };\n    case \"1\";\n    case \"4\";\n    case \"2\": { hint str [\"2\", _var] };\n};\n\n_var = \"0\"; // -> [\"3\", \"0\"]\n_var = \"1\"; // -> [\"2\", \"1\"]\n_var = \"2\"; // -> [\"2\", \"2\"]\n_var = \"3\"; // -> [\"3\", \"3\"]\n_var = \"4\"; // -> [\"2\", \"4\"]\n_var = \"5\"; // -> [\"default\", \"5\"]\n```","code":["switch _var do\n{\n\tcase \"0\";\n\tdefault { hint str [\"default\", _var] };\n\tcase \"3\": { hint str [\"3\", _var] };\n\tcase \"1\";\n\tcase \"4\";\n\tcase \"2\": { hint str [\"2\", _var] };\n};\n\n_var = \"0\"; // -> [\"3\", \"0\"]\n_var = \"1\"; // -> [\"2\", \"1\"]\n_var = \"2\"; // -> [\"2\", \"2\"]\n_var = \"3\"; // -> [\"3\", \"3\"]\n_var = \"4\"; // -> [\"2\", \"4\"]\n_var = \"5\"; // -> [\"default\", \"5\"]"]}],"multiplayer":null,"problems":null,"aliases":[],"see_also":["Control Structures","a:b","case","default"],"notes":[{"user":"ColonelSandersLite","date":"2007-08-07T07:49:00Z","text":"Be careful of the parentheses around the variable you're switching on.\nIf you accidentally use braces instead (e.g: `switch { _myVar } do { /*... */ }`), it won't error, but will always return default.\n\n> [!NOTE]\n> This is due to `{ _myvar }` being Code."},{"user":"Iva","date":"2009-10-06T14:43:00Z","text":"It's possible to use Boolean value as a switch and Code as case. One thing to take special care in such case is that code must be in parentheses. Example:\n\n```sqf\nswitch (true) do\n{\n    case (_boolVar): { someCode };\n    case (unit1 distance unit2 > 5): { someCode };\n};\n```"},{"user":"Freddo3000","date":"2020-11-01T10:05:00Z","text":"You are able to call code within the switch statements, such as:\n\n```sqf\nprivate _myFunc = {\n    systemChat \"0\";\n    case 1: { systemChat \"1\" };\n    systemChat \"2\";\n};\n\nprivate _myParameter = 1;\n\nswitch (_myParameter) do\n{\n    _myParameter call _myFunc;\n    case 1: { systemChat \"4\" };\n    systemChat \"3\";\n    default { systemChat \"-1\"; };\n};\n```\n\nHowever the order in which it is executed is difficult to make sense of, see the code comment.\n\nThis is used by BIS_fnc_missionConversations and BIS_fnc_missionTasks"}],"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"]}