switch do: Difference between revisions
Jump to navigation
Jump to search
Freddo3000 (talk | contribs) mNo edit summary |
(Moved some of the notes into the description. Removed old notes which are now part of either the description or the examples. Formatted the code in the notes.) |
||
Line 15: | Line 15: | ||
If a case has no code block, the code of the next case will automatically be executed. This makes it possible to formulate a logical '''"or"''' for cases which otherwise would contain the exact same code. (See example 4 below)<br> | If a case has no code block, the code of the next case will automatically be executed. This makes it possible to formulate a logical '''"or"''' for cases which otherwise would contain the exact same code. (See example 4 below)<br> | ||
The [[default]] block will be executed '''only if''' no case matches, no matter at which position inside the switch it is. It is not a case, and will '''never''' be entered by fallthrough.<br> | The [[default]] block will be executed '''only if''' no case matches, no matter at which position inside the switch it is. It is not a case, and will '''never''' be entered by fallthrough.<br> | ||
<br> | |||
{{Informative|[[String]] comparison is case-sensitive. Use [[toUpper]], [[toLower]], [[toLowerANSI]] or [[toUpperANSI]] to force all [[String|strings]] to the same case.}}</br> | |||
{{Informative|If no [[default]] block is provided and no [[case]] is matched, the default value [[true]] is returned. Otherwise it returns whatever the valid case block returns.}} | |||
|DESCRIPTION= | |||
____________________________________________________________________________________________ | ____________________________________________________________________________________________ | ||
Line 76: | Line 78: | ||
}} | }} | ||
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]] | |||
[[Category:Scripting Commands Armed Assault|{{uc:{{PAGENAME}}}}]] | |||
<h3 style="display:none">Notes</h3> | <h3 style="display:none">Notes</h3> | ||
Line 84: | Line 89: | ||
<dt class="note">[[User:ColonelSandersLite|ColonelSandersLite]]<dd class="note"><br> | <dt class="note">[[User:ColonelSandersLite|ColonelSandersLite]]<dd class="note"><br> | ||
Be careful of the parenthesis around the variable you're switching on. If you accidentally use braces instead (ex: switch {_myVar} do{...), it won't error, but will always return default.<br> | Be careful of the parenthesis around the variable you're switching on. If you accidentally use braces instead (ex: switch {_myVar} do{...), it won't error, but will always return default.<br> | ||
<dd class="notedate">Posted on 06 Oct, 2009 | <dd class="notedate">Posted on 06 Oct, 2009 | ||
Line 110: | Line 104: | ||
<!-- Note Section END --> | <!-- Note Section END --> | ||
</dl> | </dl> | ||
<!-- CONTINUE Notes --> | <!-- CONTINUE Notes --> | ||
Line 124: | Line 113: | ||
<br>Sometimes, and I'm unsure why, numbers are treated differently (A2OA 1.63) with quote-wraps that worked | <br>Sometimes, and I'm unsure why, numbers are treated differently (A2OA 1.63) with quote-wraps that worked | ||
<br>in earlier code now not working unless quote-wraps are removed, with otherwise identical code. | <br>in earlier code now not working unless quote-wraps are removed, with otherwise identical code. | ||
<br><code> | <br> | ||
_number1 = (floor random 10) | <code>_number1 [[=]] ([[floor]] [[random]] 10) | ||
switch (_number1) do | [[switch]] (_number1) [[do]] | ||
{ | { | ||
case "0": | [[case]] "0"[[:]] | ||
{ | { | ||
_[[vehicle]] [[set]]objecttexture[0,"\mymodpath\textures\num1.paa"]; | |||
}; | }; | ||
//etc | //etc | ||
Line 139: | Line 128: | ||
<br> | <br> | ||
<code> | <code> | ||
switch (_number1) do | [[switch]] (_number1) [[do]] | ||
{ | { | ||
case 0: | [[case]] 0[[:]] | ||
{ | { | ||
_[[vehicle]] [[set]]objecttexture[0,"\mymodpath\textures\num1.paa"]; | |||
}; | }; | ||
//etc | //etc | ||
}; | }; | ||
</code> | </code> | ||
</dd> | </dd> | ||
</dl> | </dl> | ||
Line 184: | Line 148: | ||
You are able to call code within the switch statements, such as: | You are able to call code within the switch statements, such as: | ||
<code>_myFunc = { | <code>_myFunc [[=]] { | ||
systemChat "0"; | [[systemChat]] "0"; | ||
case 1: {systemChat "1"}; | [[case]] 1: {[[systemChat]] "1"}; | ||
systemChat "2"; | [[systemChat]] "2"; | ||
}; | }; | ||
_myParameter = 1; | _myParameter [[=]] 1; | ||
switch (_myParameter) do { | [[switch]] (_myParameter) [[do]] { | ||
_myParameter call _myFunc; | _myParameter [[call]] _myFunc; | ||
case 1: {systemChat "4"}; | [[case]] 1: {[[systemChat]] "4"}; | ||
systemChat "3"; | [[systemChat]] "3"; | ||
default {systemChat "-1";}; | [[default]] {[[systemChat]] "-1";}; | ||
}; | };</code> | ||
However the order in which it is executed is difficult to make sense of, see the code comment. | However the order in which it is executed is difficult to make sense of, see the code comment. |
Revision as of 14:37, 1 November 2020
Description
- 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.
If a case has no code block, the code of the next case will automatically be executed. This makes it possible to formulate a logical "or" for cases which otherwise would contain the exact same code. (See example 4 below)
The default block will be executed only if no case matches, no matter at which position inside the switch it is. It is not a case, and will never be entered by fallthrough.
- Groups:
- Program Flow
Syntax
- Syntax:
- switch do block
- Parameters:
- switch: Switch Type
- block: Code
- Return Value:
- Anything or true
Examples
- Example 1:
switch (_a) do { case 1 : { /*...code...*/ }; case 2 : { /*...code...*/ }; default { /*...code...*/ }; };
switch (_condition) do { case 1: { hint "1" }; case 2: { hint "2" }; default { hint "default" }; };
switch (_condition) do { case "string1"; case "string2": { hint "string1 or string2" }; case "string3"; case "string4": { hint "string3 or string4" }; default { hint "default" }; };
- Example 2:
_color = switch (side player) do { case west: { "ColorGreen" }; case east: { "ColorRed" }; };
- Example 3:
_fn_moveForward = { /*...code...*/ }; _fn_moveBackward = { /*...code...*/ }; _fn_invalidKey = { /*...code...*/ }; switch true do { case (_dikCode in actionKeys "MoveForward"): _fn_moveForward; case (_dikCode in actionKeys "MoveBackward"): _fn_moveBackward; default _fn_invalidKey; };
- Example 4:
switch _var do { case "0"; default { hint str ["default", _var] }; case "3": { hint str ["3", _var] }; case "1"; case "4"; case "2": { hint str ["2", _var] }; };
_var = "0"; //-> ["3", "0"] _var = "1"; //-> ["2", "1"] _var = "2"; //-> ["2", "2"] _var = "3"; //-> ["3", "3"] _var = "4"; //-> ["2", "4"] _var = "5"; //-> ["default", "5"]
Additional Information
- See also:
- Control Structuresa:bcasedefault
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Notes
- Posted on 07 Aug, 2008
- ColonelSandersLite
Be careful of the parenthesis around the variable you're switching on. If you accidentally use braces instead (ex: switch {_myVar} do{...), it won't error, but will always return default.
- Posted on 06 Oct, 2009
- Iva
-
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:
switch (true) do { case (_boolVar): {someCode}; case (unit1 distance unit2 > 5): {someCode}; };
- Posted on November 6, 2014 - 16:33 (UTC)
- Eggbeast
-
BEWARE:
Sometimes, and I'm unsure why, numbers are treated differently (A2OA 1.63) with quote-wraps that worked
in earlier code now not working unless quote-wraps are removed, with otherwise identical code.
_number1 = (floor random 10) switch (_number1) do { case "0"[[:]] { _vehicle setobjecttexture[0,"\mymodpath\textures\num1.paa"]; }; //etc };
this one below works, and the one above stopped working with 1.63
switch (_number1) do { case 0[[:]] { _vehicle setobjecttexture[0,"\mymodpath\textures\num1.paa"]; }; //etc };
- Posted on Nobember 1, 2020 - 10:05 (UTC)
- Freddo3000
-
You are able to call code within the switch statements, such as:
_myFunc = { systemChat "0"; case 1: {systemChat "1"}; systemChat "2"; }; _myParameter = 1; switch (_myParameter) do { _myParameter call _myFunc; case 1: {systemChat "4"}; systemChat "3"; default {systemChat "-1";}; };
However the order in which it is executed is difficult to make sense of, see the code comment. This is used by BIS_fnc_missionConversations and BIS_fnc_missionTasks