IF ELSE V/S SWITCH STATEMENT
BY IF ELSE STATEMENT
package com.cnc;
public class Main {
public static void main(String[] args) {
printDayOfTheWeek(3);
}
public static void printDayOfTheWeek(int day) {
if (day == 0) {
System.out.println("sunday");
} else if (day == 1) {
System.out.println("monday");
} else if (day == 2) {
System.out.println("tuesday");
} else if (day == 3) {
System.out.println("wednesday");
} else if (day == 4) {
System.out.println("thursday");
} else if (day == 5) {
System.out.println("friday");
} else if (day == 7) {
System.out.println("saturday");
} else {
System.out.println("invalid");
}
}
}
BY SWITCH CASE STATEMENT
public class Main {
public static void main(String[] args) {
printDayOfTheWeek(3);
}
public static void printDayOfTheWeek(int day) {
switch (day){
case 0:
System.out.println("TODAY IS SUNDAY");
break;
case 1:
System.out.println("TODAY IS MONDAY");
break;
case 2:
System.out.println("TODAY IS TUESDAY");
break;
case 3:
System.out.println("TODAY IS WEDNESDAY");
break;
case 4:
System.out.println("TODAY IS THURSDAY");
break;
case 5:
System.out.println("TODAY IS FRIDAY");
break;
case 6:
System.out.println("TODAY IS SATURDAY");
break;
default:
System.out.println("Invalid day");
}
}
}