Switch in OpenSCAD

Based on the provided input, I have identified the language as OpenSCAD and will now transform the given code example into OpenSCAD.

Switch statements

Switch statements express conditionals across many branches.

Here’s a basic switch.

i = 2;
echo("Write ", i, " as ");
switch (i) {
    case 1:
        echo("one");
    case 2:
        echo("two");
    case 3:
        echo("three");
}

You can use commas to separate multiple expressions in the same case statement. We use the optional default case in this example as well.

day = weekday();
switch (day) {
    case 6, 7:
        echo("It's the weekend");
    default:
        echo("It's a weekday");
}

switch without an expression is an alternate way to express if/else logic. Here we also show how the case expressions can be non-constants.

hour = hour();
switch () {
    case (hour < 12):
        echo("It's before noon");
    default:
        echo("It's after noon");
}

A type switch compares types instead of values. Although OpenSCAD does not have a direct type switch concept, we can achieve similar functionality using nested conditionals.

function whatAmI(i) =
    is_bool(i) ? "I'm a bool" :
    is_int(i) ? "I'm an int" :
    str("Don't know type ", typeof(i));

echo(whatAmI(true));
echo(whatAmI(1));
echo(whatAmI("hey"));

To run the OpenSCAD script, place the code in a .scad file and open it with OpenSCAD. The echo statements will display the output in the OpenSCAD console.

Now that we can run and build basic OpenSCAD programs, let’s learn more about the language.

Next example: Arrays.