switch ( variable ){
case value :
// code block
case value :
// code block
case value :
// code block
}
There is a list of food to be served based on different weekdays in a hostel canteen. It is clear that one of the food type is going to return for any input weekday, so it is better to use switch condition to match the current day with the weekday and return the food type of the day.
int month=3;
switch ( month) {
case 1:
System.out.println("Jan : " + month);
break;
case 2:
System.out.println("Feb : " + month);
break;
case 3:
System.out.println("March: " + month);
break;
}
If our month is not within these three months then a different message has to be returned. For this we will add default command.
int month=5;
switch ( month) {
case 1:
System.out.println("Jan : " + month);
break;
case 2:
System.out.println("Feb : " + month);
break;
case 3:
System.out.println("March: " + month);
break;
default:
System.out.println("No matching months");
}

var emp_name="Rama";
switch ( emp_name) {
case "Rabi":
System.out.println("Rabi is a Manager");
break;
case "Raju":
System.out.println("Raju is a Driver");
break;
case "Rama":
System.out.println("Rama is an officer");
break;
default:
System.out.println("No Employee found");
}
Output is here
Rama is an officer
int month=8;
switch ( month) {
case 4:
case 5:
case 6:
System.out.println("First Quarter");
break;
case 7:
case 8:
case 9:
System.out.println("Second Quarter");
break;
case 10:
case 11:
case 12:
System.out.println("Third Quarter");
break;
case 1:
case 2:
case 3:
System.out.println("Fourth Quarter");
break;
default:
System.out.println("Wrong Month Number");
}
Output
Second Quarter
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.