The GNU case range extension allows case ranges in switch statements:
switch (value) {
case 1 ... 8:
printf(\"Hello, 1 to 8\\n\");
break;
Alternatively, since the numbers are adjacent to each other, you can do a manual optimization of the switch-case.
typedef void(*func_t)(void);
#define CASES_N 9
void func0 (void)
{
printf("Hello, 0\n");
}
void func1 (void)
{
printf("Hello, 1 to 8\n");
}
static const func_t execute [CASES_N] =
{
&func0,
&func1,
&func1,
&func1,
&func1,
&func1,
&func1,
&func1,
&func1
};
int main()
{
if(what < CASES_N)
{
execute[what]();
}
else
{
printf("Hello, default\n");
}
}
This code is basically the same as a compiler-optimized switch-statement.