Java Tutorial/Statement Control/Switch Statement

Материал из Java эксперт
Перейти к: навигация, поиск

Execute the same statements for several different case labels

   <source lang="java">

public class MainClass {

 public static void main(String[] args) {
   char yesNo = "N";
   switch(yesNo) {
     case "n": case "N":
          System.out.println("No selected");
          break;
     case "y": case "Y":
          System.out.println("Yes selected");
          break;
   }
 }

}</source>



No selected


Free Flowing Switch Statement Example

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   int i = 0;
   switch (i) {
   case 0:
     System.out.println("i is 0");
   case 1:
     System.out.println("i is 1");
   case 2:
     System.out.println("i is 2");
   default:
     System.out.println("Free flowing switch example!");
   }
 }

} /* i is 0 i is 1 i is 2 Free flowing switch example!

  • /</source>





Nested Switch Statements Example

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   int i = 0;
   switch (i) {
   case 0:
     int j = 1;
     switch (j) {
     case 0:
       System.out.println("i is 0, j is 0");
       break;
     case 1:
       System.out.println("i is 0, j is 1");
       break;
     default:
       System.out.println("nested default case!!");
     }
     break;
   default:
     System.out.println("No matching case found!!");
   }
 }

} //i is 0, j is 1</source>





Switch statement with enum

   <source lang="java">

public class MainClass {

 enum Choice { Choice1, Choice2, Choice3 }
 public static void main(String[] args) {
   Choice ch = Choice.Choice1;
   switch(ch) {
     case Choice1:
       System.out.println("Choice1 selected");
       break;
    case Choice2:
      System.out.println("Choice2 selected");
      break;
    case Choice3:
      System.out.println("Choice3 selected");
      break;
   }
 }

}</source>



Choice1 selected


The switch Statement

  1. An alternative to a series of else if is the switch statement.
  2. The switch statement allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.
  3. The expression used in the switch statement must return an int or an enumerated value.

The syntax of the switch statement is as follows.



   <source lang="java">

switch (expression) { case value_1 :

    statement (s);
    break;

case value_2 :

    statement (s);
    break;
 .
 .
 .

case value_n :

    statement (s);
    break;

default:

    statement (s);

}</source>





The switch Statement: a demo

   <source lang="java">

public class MainClass {

 public static void main(String[] args) {
   int choice = 2;
   switch (choice) {
   case 1:
     System.out.println("Choice 1 selected");
     break;
   case 2:
     System.out.println("Choice 2 selected");
     break;
   case 3:
     System.out.println("Choice 3 selected");
     break;
   default:
     System.out.println("Default");
     break;
   }
 }

}</source>



Choice 2 selected