switch: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\|seealso= *\[\[([^ ]+)\]\], \[\[([^ ]+)\]\]" to "|seealso= $1 $2")
m (Text replacement - "{{Feature|Informative|" to "{{Feature|informative|")
 
(6 intermediate revisions by the same user not shown)
Line 22: Line 22:


|descr= 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.<br>
|descr= 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.<br>
If 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 {{HashLink|#Example 2}}).<br>
If 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 {{Link|#Example 2}}).<br>
The [[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.
The [[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.


{{Feature|Informative|
{{Feature|informative|
* [[String]] comparison is case-sensitive. Use [[toUpper]], [[toLower]], [[toLowerANSI]] or [[toUpperANSI]] to force all [[String|strings]] to the same case.
* [[String]] comparison is case-sensitive. Use [[toUpper]], [[toLower]], [[toLowerANSI]] or [[toUpperANSI]] to force all [[String|strings]] to the same case.
* 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.
* 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.
Line 36: Line 36:
|r1= [[Switch Type]]
|r1= [[Switch Type]]


|x1= <code>[[switch]] ([[floor]] [[random]] 5) [[do]]
|x1= <sqf>
switch (floor random 5) do
{
{
[[case]] 1: { [[hint]] "one"; };
case 1: { hint "one"; };
[[case]] 2: { [[hint]] "two"; };
case 2: { hint "two"; };
[[default]] { [[hint]] "zero, three or four" };
default { hint "zero, three or four" };
};</code>
};
</sqf>


|x2= <!-- this example is referenced in Description -->
|x2= <!-- this example is referenced in Description -->
<code>[[switch]] (_condition) [[do]]
<sqf>
switch (_condition) do
{
{
[[case]] "string1";
case "string1";
[[case]] "string2": { [[hint]] "string1 or string2" };
case "string2": { hint "string1 or string2" };
[[case]] "string3";
case "string3";
[[case]] "string4": { [[hint]] "string3 or string4" };
case "string4": { hint "string3 or string4" };
[[case]] "string5"; {{cc|will '''never''' enter default's code}}
case "string5"; // will never enter default's code
[[default]] { [[hint]] "default" };
default { hint "default" };
};
};
</code>
</sqf>


|x3= <code>[[private]] _color = [[switch]] ([[side]] [[player]]) [[do]]
|x3= <sqf>
private _color = switch (side player) do
{
{
[[case]] [[west]]: { "ColorGreen" };
case west: { "ColorGreen" };
[[case]] [[east]]: { "ColorRed" };
case east: { "ColorRed" };
};
};
</code>
</sqf>


|x4= <code>[[private]] _fn_moveForward = { {{codecomment|/*...code...*/}} };
|x4= <sqf>
[[private]] _fn_moveBackward = { {{codecomment|/*...code...*/}} };
private _fn_moveForward = { /*...code...*/ };
[[private]] _fn_invalidKey = { {{codecomment|/*...code...*/}} };
private _fn_moveBackward = { /*...code...*/ };
private _fn_invalidKey = { /*...code...*/ };


[[switch]] [[true]] [[do]]
switch true do
{
{
[[case]] (_dikCode [[in]] [[actionKeys]] "MoveForward"): _fn_moveForward;
case (_dikCode in actionKeys "MoveForward"): _fn_moveForward;
[[case]] (_dikCode [[in]] [[actionKeys]] "MoveBackward"): _fn_moveBackward;
case (_dikCode in actionKeys "MoveBackward"): _fn_moveBackward;
[[default]] _fn_invalidKey;
default _fn_invalidKey;
};
};
</code>
</sqf>


|x5= <code>[[switch]] _var [[do]]
|x5= <sqf>
switch _var do
{
{
[[case]] "0";
case "0";
[[default]] { [[hint]] [[str]] ["default", _var] };
default { hint str ["default", _var] };
[[case]] "3": { [[hint]] [[str]] ["3", _var] };
case "3": { hint str ["3", _var] };
[[case]] "1";
case "1";
[[case]] "4";
case "4";
[[case]] "2": { [[hint]] [[str]] ["2", _var] };
case "2": { hint str ["2", _var] };
};
};


_var = "0"; {{cc|-> ["3", "0"]}}
_var = "0"; // -> ["3", "0"]
_var = "1"; {{cc|-> ["2", "1"]}}
_var = "1"; // -> ["2", "1"]
_var = "2"; {{cc|-> ["2", "2"]}}
_var = "2"; // -> ["2", "2"]
_var = "3"; {{cc|-> ["3", "3"]}}
_var = "3"; // -> ["3", "3"]
_var = "4"; {{cc|-> ["2", "4"]}}
_var = "4"; // -> ["2", "4"]
_var = "5"; {{cc|-> ["default", "5"]}}
_var = "5"; // -> ["default", "5"]
</code>
</sqf>


|seealso= [[Control Structures]] [[a:b]], [[case]], [[default]]
|seealso= [[Control Structures]] [[a:b]] [[case]] [[default]]
}}
}}


Line 98: Line 104:
|user= ColonelSandersLite
|user= ColonelSandersLite
|timestamp= 20070807074900
|timestamp= 20070807074900
|text= <br>Be careful of the parentheses around the variable you're switching on.
|text= Be careful of the parentheses 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.
If you accidentally use braces instead (e.g: <sqf inline>switch { _myVar } do { /*... */ }</sqf>), it won't error, but will always return default.
{{Feature|informative|This is due to {{ic|{_myvar}<nowiki/>}} being [[Code]].}}
{{Feature|informative|This is due to <sqf inline>{ _myvar }</sqf> being [[Code]].}}
}}
}}


Line 107: Line 113:
|timestamp= 20091006144300
|timestamp= 20091006144300
|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:
|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:
<code>[[switch]] ([[true]]) [[do]]
<sqf>
switch (true) do
{
{
[[case]] (_boolVar): { someCode };
case (_boolVar): { someCode };
[[case]] (unit1 [[distance]] unit2 > 5): { someCode };
case (unit1 distance unit2 > 5): { someCode };
};
};
</code>
</sqf>
}}
}}


Line 120: Line 127:
|text= You are able to call code within the switch statements, such as:
|text= You are able to call code within the switch statements, such as:


<code>[[private]] _myFunc [[=]] {
<sqf>
[[systemChat]] "0";
private _myFunc = {
[[case]] 1: { [[systemChat]] "1" };
systemChat "0";
[[systemChat]] "2";
case 1: { systemChat "1" };
systemChat "2";
};
};


[[private]] _myParameter [[=]] 1;
private _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>
};
</sqf>


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.

Latest revision as of 00:26, 2 February 2024

Hover & click on the images for description

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, making it possible to formulate a logical "or" for cases which would otherwise contain the exact same code (see Example 2).
The 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.
Groups:
Program Flow

Syntax

Syntax:
switch value
Parameters:
value: Anything
Return Value:
Switch Type

Examples

Example 1:
switch (floor random 5) do { case 1: { hint "one"; }; case 2: { hint "two"; }; default { hint "zero, three or four" }; };
Example 2:
switch (_condition) do { case "string1"; case "string2": { hint "string1 or string2" }; case "string3"; case "string4": { hint "string3 or string4" }; case "string5"; // will never enter default's code default { hint "default" }; };
Example 3:
private _color = switch (side player) do { case west: { "ColorGreen" }; case east: { "ColorRed" }; };
Example 4:
private _fn_moveForward = { /*...code...*/ }; private _fn_moveBackward = { /*...code...*/ }; private _fn_invalidKey = { /*...code...*/ }; switch true do { case (_dikCode in actionKeys "MoveForward"): _fn_moveForward; case (_dikCode in actionKeys "MoveBackward"): _fn_moveBackward; default _fn_invalidKey; };
Example 5:
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 Structures a:b case default

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
ColonelSandersLite - c
Posted on Aug 07, 2007 - 07:49 (UTC)
Be careful of the parentheses around the variable you're switching on. If you accidentally use braces instead (e.g: switch { _myVar } do { /*... */ }), it won't error, but will always return default.
This is due to { _myvar } being Code.
Iva - c
Posted on Oct 06, 2009 - 14:43 (UTC)
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 }; };
Freddo3000 - c
Posted on Nov 01, 2020 - 10:05 (UTC)
You are able to call code within the switch statements, such as:
private _myFunc = { systemChat "0"; case 1: { systemChat "1" }; systemChat "2"; }; private _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