CONTENTS | PREV | NEXT Java Code Conventions


5.8 switch Statements

A switch statement should have the following form:

switch (condition) {
case ABC:
statements;
/* falls through */
case DEF:
statements;
break;

case XYZ:
statements;
break;

default:
statements;
break;
}
Every time a case falls through (doesn't include a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the  /* falls through */ comment.

Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added.



CONTENTS | PREV | NEXT
Copyright © 1997 Sun Microsystems, Inc. All Rights Reserved.