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?
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development