You are previewing Code Complete, Second Edition.

Code Complete, Second Edition

Cover of Code Complete, Second Edition by Steve McConnell Published by Microsoft Press

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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.