Switch in UnrealScript

Switch statements express conditionals across many branches.

Here’s a basic switch.

class SwitchExample extends Actor;

simulated function BeginPlay()
{
    local int i;
    
    i = 2;
    `Log("Write" @ i @ " as");
    switch (i)
    {
        case 1:
            `Log("one");
            break;
        case 2:
            `Log("two");
            break;
        case 3:
            `Log("three");
            break;
    }

    // Multiple expressions in same case statement and default case
    switch (WorldInfo.TimeSeconds % 7)
    {
        case 0, 6:
            `Log("It's the weekend");
            break;
        default:
            `Log("It's a weekday");
            break;
    }
    
    // Alternate way to express if/else logic, case expressions can be non-constants
    switch
    {
        case (WorldInfo.TimeSeconds % 24) < 12:
            `Log("It's before noon");
            break;
        default:
            `Log("It's after noon");
            break;
    }

    // Type switch to discover the type of a variable
    whatAmI("hello");
    whatAmI(42);
    whatAmI(true);
}

function whatAmI(Object obj)
{
    switch(obj)
    {
        default:
            `Log("Unknown type");
            break;
        case string:
            `Log("I'm a string");
            break;
        case int:
            `Log("I'm an int");
            break;
        case bool:
            `Log("I'm a bool");
            break;
    }
}

To run the program, compile the Unrealscript code and debug it within the Unreal Engine environment.