Executes one case from a list of mutually exclusive candidates.

Switch SwitchValue
Case CaseValue1:
Case CaseValue2a, CaseValue2b:


If present, SwitchValue 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 true (non-zero and non-empty) is executed. If there is no matching case and a Default is present, it is executed.

String comparisons are case sensitive. StrLower or StrUpper can be used to emulate case insensitive comparison. For example, Switch StrLower(Value), where each case uses a lower-case string.

As with the = and == operators, numeric comparison is performed if SwitchValue and the case value are both pure numbers, or if one is a pure number and the other is a numeric string. Each case is considered separately and does not affect the type of comparison used for other cases.

Each case may list up to 20 values. Each value must be an expression, but can be a simple one such as a literal number, quoted string or variable. Case and Default must be terminated with a colon :.

The first statement of each case may be below Case or on the same line, following the colon. Each case implicitly ends at the next Case/Default or the closing brace. Unlike the switch statement found in some other languages, there is no implicit fall-through and Break is not used (except to break out of an enclosing loop).

As all cases are enclosed in the same block, a label defined in one case can be the target of Goto from another case. However, if a label is placed immediately above Case or Default, it targets the end of the previous case, not the beginning of the next one.

Default is not required to be listed last.


If, Else, Blocks


#1: To test this example, type [ followed by one of the abbreviations listed below, any other 5 characters, or Enter/Esc/Tab/.; or wait for 4 seconds.

    Hook := InputHook("V T5 L4 C", "{enter}.{esc}{tab}", "btw,otoh,fl,ahk,ca")
    switch Hook.EndReason
    case "Max":
        MsgBox 'You entered "' Hook.Input '", which is the maximum length of text.'
    case "Timeout":
        MsgBox 'You entered "' Hook.Input '" at which time the input timed out.'
    case "EndKey":
        MsgBox 'You entered "' Hook.Input '" and terminated the input with ' Hook.EndReason '.'
    default:  ; Match
        switch Hook.Input
        case "btw":   Send "{backspace 3}by the way"
        case "otoh":  Send "{backspace 4}on the other hand"
        case "fl":    Send "{backspace 2}Florida"
        case "ca":    Send "{backspace 2}California"
        case "ahk":
            Send "{backspace 3}"
            Run "https://www.autohotkey.com"