O'Reilly logo

Oracle Certified Associate, Java SE 7 Programmer Study Guide by Richard M. Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The switch statement

The purpose of a switch statement is to provide a convenient and simple method of making multi-branch selections based upon integer, enumeration, or String expression. The switch statement has the following basic form:

switch ( expression ) {
  //case clauses
}

There are usually multiple case clauses within the statement block. The basic form of the case clause uses the case keyword followed by a colon, zero or more statements, and usually a break statement. The break statement consists of a single keyword, break, shown as follows:

case <constant-expression>:
  //statements
break;

There is also an optional default clause that can be used. This will catch any values not caught by a case clause. This is demonstrated as follows:

default: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required