giovedì 28 marzo 2013

costrutto CASE

CASE


Il costrutto switch case funzoina in questo modo : a seconda del confronto eseguito nello switch si entra nel flusso di esecuzione nel case appostito. Dopo di che si prosegue fino in fondo e si esce solo se incontra un break. Prendiamo questo esempio :

public class Principale {
       public static void main(String[] args) {
             String color = "Red";
             switch (color) {
             case "Red":
                    System.out.println("Found Red");
             case "Blue":
                    System.out.println("Found Blue");
                    break;
             case "White":
                    System.out.println("Found White");
                    break;
             default:
                    System.out.println("Found Default");
             }
       }
}
Se si ha Red nello switch si entra in Red e si esce al break :

Found Red
Found Blue

Se si ha Bluenello switch si entra in Bluee si esce al break :
Found Blue

Nessun commento:

Posta un commento