Cold Help System: Programming: ColdC Reference Manual: Language Structure: Statements: Conditional: switch


The switch statement is used to compare one value against a series of other values. The switch statement is the most complicated statement in ColdC, and does vary from it's counterpart in C, so rather than using abstract explanations we will start with an example:

switch (val) {
    case 0:
        cwrite("The value is zero.");
    case 1 .. 10:
        cwrite("The value is between one and ten inclusive.");
    case 11 .. a:
        cwrite("The value is between eleven and a inclusive.");
    case "foo", "bar".."baz":
        cwrite("The value is \"foo\" or between \"bar\" and \"baz\"");
    case a .. b, c .. d, 42:
        count = count + 1;
        cwrite("The value is in the counted area.");
    case ~perm:
        cwrite("Permission denied while getting the value.");
    default:
        cwrite("Did not recognize value.");
}

This example illustrates all of the capabilities of the switch statement. The expression given by val in the example is the controlling expression, and is compared against each of the cases inside the switch body until a match is found. Each case has a value or list of values to compare against. The values can be of any type, and need not be constant expressions. Ranges are specified using two dots (..) to separate the lower and upper bounds. The keyword default specifies an action to perform if no cases were matched by the controlling expression.

Here is a more formal description of the syntax of the switch statement:

switch (controlling-expression) {
case expr-or-range, expr-or-range, ..:
statement
case expr-or-range, expr-or-range, ..:
statement
..
default:
default-statement
}

When executing a switch statement, the interpreter scans through the list of cases and compares the controlling-expression against each of the cases, evaluating the case from left to right until there is a match. When using a range, the lower and upper bounds must be of the same type and must be either integers or strings. If they are not the error ~type is thrown. When the interpreter finds a match, it will execute the statement for that case. The interpreter will not continue checking cases after a match.

If the interpreter does not find a match, it will execute instead execute the default-statement. A default statement does not need to be defined. If a default is not defined nothing in the switch is executed.

C programmers should note that switch statements in ColdC differ from switch statements in C in several respects. Because case values do not have to be constants, they may conflict, in which case the first match will take precedence. Also, there is no fall-through in ColdC switch statements; only the statements corresponding to the matching case will be executed. Because there is no fall-through, the break statement does not apply to switch statements. Finally, the default case must be placed last in the list of cases if it is given.


if | if-else | switch


the Cold Dark