case Statements

The case or switch statement is a construct that varies a great deal from language to language. C++ and Java support case only for ordinal types taken one value at a time. Visual Basic supports case for ordinal types and has powerful shorthand notations for expressing ranges and combinations of values. Many scripting languages don't support case statements at all.

The following sections present guidelines for using case statements effectively:

Choosing the Most Effective Ordering of Cases

You can choose from among a variety of ways to organize the cases in a case statement. If you have a small case statement with three options and three corresponding lines of code, the order you use doesn't matter much. If you have a long case statement—for ...

Get Code Complete, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.