### Question 20.18

Is there a way to have non-constant `case` labels (i.e. ranges or arbitrary expressions)?

No. The `switch` statement was originally designed to be quite simple for the compiler to translate, therefore case labels are limited to single, constant, integral expressions. You can attach several case labels to the same statement, which will let you cover a small range if you don't mind listing all cases explicitly.

If you want to select on arbitrary ranges or non-constant expressions, you'll have to use an if/else chain.

See also questions question 20.17.

References: K&R1 Sec. 3.4 p. 55
K&R2 Sec. 3.4 p. 58
ANSI Sec. 3.6.4.2
ISO Sec. 6.6.4.2
Rationale Sec. 3.6.4.2
H&S Sec. 8.7 p. 248

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback