Ranges in case statements

If you disable the ANSI Strict setting, the compiler allows ranges of values in a switch statement by using a special form of case statement. A case statement that uses a range is a shorter way of specifying consecutive case statements that span a range of values.

The range form of a case statement is

case low ... high : // note spaces around ellipsis character

where low is a valid case expression that is less than high, which is also a valid case expression. A case statement that uses a range is applied when the expression of a switch statement is both greater than or equal to the low expression and less than or equal to the high expression.

NOTE Make sure to separate the ellipsis (...) from the low and high expressions with spaces.

Listing 1. Ranges in case Statements

switch (i)
{
case 0 ... 2: /* Equivalent to case 0: case 1: case 2: */
j = i * 2;
break;
case 3:
j = i;
break;
default:
j = 0;
break;
}