Machine-translated by Google. Questions are authored in English.
Top

Discussion

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?

  • A.Yes
  • B.No

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.