Next: Looping Statements

Prev: Simple Statements

Conditional Statements

There are three types of conditional statements in C--. The if statement has the following syntax:

if (expression)
    statement
The interpreter evaluates expression. If the value of expression is true (see Data Types), then the interpreter executes statement.

The if-else statement is similar to the if statement. The if-else statement has the following syntax:

if (expression)
    true-statement
else
    false-statement
The interpreter evaluates expression, as before. If the value of expression is true, then the interpreter executes true-statement; otherwise, it executes false-statement.

Because the if statement and the if-else statement are similar, they can sometimes be ambiguous. The following code will produce unexpected results:

if (a)
    if (b) c;
else
    d;
The indentation suggests that the else clause should apply to the first if clause, but in fact it applies to the more recent one. You can avoid ambiguities like this by using braces to create compound statements out of conditional and looping statements, even if there is only one of them. In this case, you might write:

if (a) {
    if (b)
        c;
} else {
    d;
}
The third type of conditional statement is the switch statement, which allows you to compare one value against a series of other values. The switch statement is the most complicated statement type in C--, so we'll start with an example:

switch (val) {
    case 0:
        echo("The value is zero.");
    case 1 .. 10:
        echo("The value is between one and ten inclusive.");
    case 11 .. a:
        echo("The value is between eleven and a inclusive.");
    case "foo", "bar".."baz":
	echo("The value is \"foo\" or between \"bar\" and \"baz\"");
    case a .. b, c .. d, 42:
        count = count + 1;
        echo("The value is in the counted area.");
    case ~perm:
        echo("Permission denied while getting the value.");
    default:
        echo("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. 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. You can also specify ranges, 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, ...:
        statements
    case expr-or-range, expr-or-range, ...:
        statements
    .
    .
    .
    default:
        default-statement
}
When executing a switch statement, the interpreter scans through the list of cases and compares controlling-expression against each of the lists of values in the cases, evaluating the value lists from left to right until there is a match. The lower and upper bounds of ranges must be of the same type and must be either integers or strings, or the interpreter will throw a ~type error. 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 default-statement, the statement corresponding to the default case. You do not need to provide a default case if you do not wish to provide a default action.

C programmers should note that switch statements in C-- 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 C-- 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.