Istruzioni switch

Java intermedio

Jim White

Java Developer

Cos’è uno switch?

  • Lo switch ha uno scopo simile a if-else
    • Esegue azioni in base a una condizione
    • Usato al posto di più if-then-else
  • Più conciso di più if-then-else
if (bonus == 10000) {
    System.out.println("Let's buy a car!");
} else if (bonus == 5000) {
    System.out.println("Let's take a trip!");
} else if (bonus == 1000) {
    System.out.println("Let's save!");
}
Java intermedio

Sintassi dello switch

switch (expression) {
    case value1:  // if (expression == value1)
        // statements
    case value2:  // if (expression == value2)
        // statements
    // use as many case statements as needed
}
Java intermedio

Esempio di switch

char direction = 'N';

switch (direction) {
    case 'E':  
        System.out.println("We are headed east.");
    case 'S':  
        System.out.println("We are headed south.");
    case 'W':  
        System.out.println("We are headed west.");
    case 'N':   
        System.out.println("We are headed north."); // This would be printed
}
Java intermedio

Break

  • Impedisce a Java di proseguire negli altri case
    • Senza break, Java esegue i case successivi finché non trova un break
char direction = 'W';

switch (direction) {
    case 'E':
        System.out.println("We are headed east.");
    case 'S':
        System.out.println("We are headed south.");
    case 'W':
        System.out.println("We are headed west."); // This would be printed
    case 'N':
        System.out.println("We are headed north."); // This would also be printed
}
Java intermedio

break permette di combinare più case:

char direction = 'N';

switch (direction) {
    case 'E':     
    case 'W':  
        System.out.println("We are headed east or west.");
        break;
    case 'S':  
    case 'N':
        System.out.println("We are headed south or north.");
        break;       
}
We are headed south or north.
Java intermedio

Default

  • Puoi aggiungere un caso default alla fine di uno switch
    • Il caso default non confronta alcun valore con l’espressione
    • Se nessun valore corrisponde, si eseguono le istruzioni di default
    • default deve essere l’ultimo caso nello switch
  • default gestisce "tutti gli altri valori"
char direction = 'z';

switch (direction) {
  case 'E':     
  case 'W':  
    System.out.println("To east or west.");
    break;
  case 'S':  
  case 'N':
    System.out.println("To south or north.");
    break; 
  default:
    System.out.println("We are lost.");
}
We are lost.
Java intermedio

Condizionali vs switch

  • I condizionali permettono lo stesso compito
  • Funzionano con ogni tipo di dato
char character = 'A';
if (character == 'A') {
    System.out.println("It's A");
} else if (direction =='B') {
    System.out.println("It's B");
} else {
    System.out.println("Unknown");
}
  • Lo switch può essere più leggibile
  • Funziona solo con alcuni tipi come int, byte, short, char o String
char character = 'A';
switch (character) {
  case 'A':
    System.out.println("It's A");
    break;
  case 'B':  
    System.out.println("It's B");
    break;
  default:
    System.out.println("Unknown");
}
Java intermedio

Passons à la pratique !

Java intermedio

Preparing Video For Download...