switch do: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Added: Example with multiple choices)
(Redirect to switch)
Tag: New redirect
 
(39 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{Command|= Comments
#REDIRECT [[switch]]
____________________________________________________________________________________________
 
| arma |= Game name
 
|1.00|= Game version
____________________________________________________________________________________________
 
| Switch form. |= Description
____________________________________________________________________________________________
 
| switch '''do''' block |= Syntax
 
|p1= switch: [[Switch Type]] |= Parameter 1
 
|p2= block: [[Code]] |= Parameter 2
 
| [[Anything]] |= Return value
____________________________________________________________________________________________
 
|x1 =  <code>[[switch]] (_condition) do
{
case 1: {[[hint]] "1"};
case 2: {[[hint]] "2"};
default {[[hint]] "default"};
};</code> |= Example 1
|x2 =  <code>[[switch]] (_condition) do
{
case "string1";
case "string2": {[[hint]] "string1 or string2"};
case "string3";
case "string4": {[[hint]] "string3 or string4"};
default {[[hint]] "default"};
};</code> |= Example 2
 
| [[Control Structures]]  |= See also
 
}}
 
<h3 style="display:none">Notes</h3>
<dl class="command_description">
<!-- Note Section BEGIN -->
 
<dd class="notedate">Posted on 07 Aug, 2008
<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>
 
 
<dd class="notedate">Posted on 11 Aug, 2008
<dt class="note">'''[[User:Dr_Eyeball|Dr_Eyeball]]'''<dd class="note">
Using [[switch]] with strings is case-sensitive, (unlike string comparisons).
 
 
<dd class="notedate">Posted on 12 Aug, 2008
<dt class="note">'''[[User:General_Barron|General Barron]]'''<dd class="note">
To be safe about the case sensitivity issue, use the [[toLower]] or [[toUpper]] command to force all strings to a certain case.
 
 
<dd class="notedate">Posted on 06 Oct, 2009
<dt class="note">'''[[User:Iva|Iva]]'''<dd class="note">
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
{
case (_boolVar): {someCode};
case (unit1 distance unit2 > 5): {someCode};
};
</code>
 
 
<!-- Note Section END -->
</dl>
 
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
[[Category:Scripting Commands ArmA|{{uc:{{PAGENAME}}}}]]
[[Category:ArmA: Control Structures|{{uc:{{PAGENAME}}}}]]
[[Category:Command Group: Program Flow|{{uc:{{PAGENAME}}}}]]

Latest revision as of 16:14, 6 June 2021

Redirect to: