Selection Statements

image

The general form of the “switch” statement is impossible to show in any meaningful form in a syntax diagram with less than about two dozen production rules. That tells you something about how badly designed the statement is right there. If you look at Kernighan and Ritchie's C book, The C Programming Language, you'll note that even they were not up to the task of showing the syntax in any better way than this:

switch (ExpressionStatement

Ignoring syntax diagrams, the switch statement is what some other languages call a “case” statement. It causes control to be transferred to one of several statements depending on the value of the Expression, which must be of type:

  • char, byte, short, or int

  • Object versions of these, Character, Byte, Short, Integer, which will be unboxed

  • An enumerated type (Chapter 6 has the scoop on enums)

The switch statement has this general appearance:

switch (Expression) {
   case constant_1 : Statement; break;
   case constant_5 :
   case constant_3 : Statement; break;
        ...
   case constant_n : Statement; break;
         default : Statement; break;
    }

The expression is evaluated. The flow of control transfers to the case clause whose constant matches the expression, if there is such a case. You can't have two cases with the same value.

The value following the keyword “case” has to be a constant. You can't use a variable as the value. Java compiler-writers have put a lot of effort into generating efficient code for switch statements, using techniques like jump tables. A jump table lets the code branch directly to the right case, without testing whether all the intervening cases match the switch variable.

Statement Notes

If you omit a “break” at the end of a branch, control falls through to execute any remaining branches after that branch is executed, up to the next break! This is almost never what you want. Implicit fall-through (in the absence of “break”) is a huge bug-prone misfeature!

There can be only one “default” branch, and it doesn't have to be last. The “default” branch can be omitted. If it is present, it is executed when none of the “case” values match the Expression.

A Statement can be labeled with several cases. (This is actually a trivial case of fall-through.) In the example above, the case for constant_5 has no statements, so execution falls through to the case for constant_3.

If none of the cases match, and there is no default case, the statement does nothing.

JDK 1.4 introduced a compiler option to check for and warn about switch fall-through! Imagine: a language feature so broken that there's now a compiler option to check that you didn't use it. It's too late to change it now because of the vast number of programs that use it. The best you could do would be to introduce a non-bug prone replacement for the switch statement and call it something different.

Use this command line option to check for broken code:

javac -Xswitchcheck filename.java

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.208.97