</head> <body> <h1>Switch <span class="ver">[v1.1.31+]</span></h1> <p>Executes one case from a list of mutually exclusive candidates.</p> <pre class="Syntax"><span class="func">Switch</span> <span class="optional">SwitchValue</span> { <span class="func">Case</span> CaseValue1: <i>Statements1</i> <span class="func">Case</span> CaseValue2a, CaseValue2b: <i>Statements2</i> <span class="func">Default</span>: <i>Statements3</i> }</pre> <h2 id="Remarks">Remarks</h2> <p>If present, <em>SwitchValue</em> is evaluated once and compared to each case value until a match is found, and then that case is executed. Otherwise, the first case which evaluates to <a href="../Concepts.htm#boolean">true</a> (non-zero and non-empty) is executed. If there is no matching case and a <code>Default</code> is present, it is executed.</p> <p><a href="StringCaseSense.htm">StringCaseSense</a> controls the case-sensitivity of string comparisons performed by Switch.</p> <p>Each case may list up to 20 values. Each value must be an <a href="../Language.htm#expressions">expression</a>, but can be a simple one such as a literal number, quoted string or variable. <code>Case</code> and <code>Default</code> must be terminated with a colon <code>:</code>.</p> <p>The first statement of each case may be below <code>Case</code> or on the same line, following the colon. Each case implicitly ends at the next <code>Case</code>/<code>Default</code> or the closing brace. Unlike the switch statement found in some other languages, there is no implicit fall-through and <a href="Break.htm">Break</a> is not used (except to break out of an enclosing loop).</p> <p>As all cases are enclosed in the same block, a label defined in one case can be the target of <a href="Goto.htm">Goto</a> from another case. However, if a label is placed immediately above <code>Case</code> or <code>Default</code>, it targets the end of the previous case, not the beginning of the next one.</p> <p><code>Default</code> is not required to be listed last.</p> <h2 id="Related">Tópicos relacionados</h2> <p><a href="IfExpression.htm">If (expression)</a>, <a href="Else.htm">Else</a>, <a href="Block.htm">Blocks</a></p> <h2 id="examples">Exemplos</h2> <div class="ex" id="ExInput"> <p><a class="ex_number" href="#ExInput"></a> This is a working hotkey example. There is a functionally equivalent <a href="Input.htm#ExHotkey">example</a> using if-else-if in the documentation for the <a href="Input.htm">Input</a> command.</p> <pre>~[:: Input, UserInput, V T5 L4 C, {enter}.{esc}{tab}, btw,otoh,fl,ahk,ca switch ErrorLevel { case "Max": MsgBox, You entered "%UserInput%", which is the maximum length of text. return case "Timeout": MsgBox, You entered "%UserInput%" at which time the input timed out. return case "NewInput": return default: if InStr(ErrorLevel, "EndKey:") { MsgBox, You entered "%UserInput%" and terminated the input with %ErrorLevel%. return } } switch UserInput { case "btw": Send, {backspace 4}by the way case "otoh": Send, {backspace 5}on the other hand case "fl": Send, {backspace 3}Florida case "ca": Send, {backspace 3}California case "ahk": Run, https://www.autohotkey.com } return </pre> </div> </body> </html>