header-logo.png GKINDEX

Java Switch Block

If you want to evaluate an expression, based on the evaluation results some piece of code needs to be executed then we have a switch block.

switch block evaluates expression based on the expression results, switch case will be executed.

If there is no switch case have the same value as what expression is resulted then default block will be executed.

Please remember switch block will be executed only once.

Java Switch Block Example

In the switch block having break key word is optional. If the break is not there then left over case blocks will be executed, in the above example case 2: block will be executed.
  • Share