We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?
Answer: A
Correct answer: Yes
A switch cannot test a range directly, but the ranges here are small and the values are integers, so each can be listed as its own label with fall-through:
case 2: case 3: case 4: /* first range */ break;
case 5: case 6: case 7: /* second range */ break;
This works because a label with no statements falls through to the next. For a wide or non-integer range, an if is the only option.
Please log in or register to report this question.