1int day = 4;
2switch (day) {
3 case 6:
4 System.out.println("Today is Saturday");
5 break;
6 case 7:
7 System.out.println("Today is Sunday");
8 break;
9 default:
10 System.out.println("Looking forward to the Weekend");
11}
12// Outputs "Looking forward to the Weekend"
1switch(x){
2 case(0): //if x == 0
3 //do some stuff
4 break;
5 //add more cases
6 default: //when x does not match any case
7 //do some stuff
8 break;
9}
1System.out.println(
2 switch (day) {
3 case MONDAY, FRIDAY, SUNDAY -> 6;
4 case TUESDAY -> 7;
5 case THURSDAY, SATURDAY -> 8;
6 case WEDNESDAY -> 9;
7 default -> throw new IllegalStateException("Invalid day: " + day);
8 }
9 );
1// syntax of switch statement in java
2switch(expression)
3{
4 case 1 value :
5 // code goes here
6 break;
7
8 case 2 value :
9 // code goes here
10 break;
11
12 case 3 value :
13 // code goes here
14 break;
15 .
16 .
17 .
18 .
19
20 default: // optional
21 // default code goes here
22}
1int age = 9;
2switch(age){
3 case 1:
4 System.out.println("You are 1 year old");
5 case 5:
6 System.out.println("You are 5 years old");
7 case 9:
8 System.out.println("You are 9 years old");
9 default:
10 System.out.println("Ain't know how many years you are");
1switch (/*Variable*/)
2{
3 case /*Variable*/:
4 /*Action*/;
5 break;
6 default:
7 /*Action*/;
8}