Added switch and ternary operators in java

This commit is contained in:
Mayank Agarwal
2023-01-30 00:15:04 +05:30
parent f040615faf
commit c518f76628
2 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
import java.util.*;
import java.time.DayOfWeek;
public class JavaSwitchStatement {
public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
break;
}
}
public static void switchExample(String[] args) {
DayOfWeek today = DayOfWeek.SATURDAY;
switch (today) {
case MONDAY:
System.out.println("Today is Monday, time to go back to work.");
break;
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
System.out.println("Today is a weekday, time to work.");
break;
case FRIDAY:
System.out.println("Today is Friday, time to finish up and relax.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Today is the weekend, time to have fun!");
break;
default:
System.out.println("Invalid day of the week.");
break;
}
}
}

View File

@@ -0,0 +1,15 @@
import java.util.*;
public class TernaryOperator {
public static void main(String[] args) {
int x = 10;
int y = (x > 5) ? 100 : 200;
}
public static void nestedTernaryOperator() {
int x = 10;
int y = 20;
int z = (x > 5) ? (y > 10 ? 30 : 40) : 50;
}
}