Skip to content

Instantly share code, notes, and snippets.

@lekeCoder
Last active April 28, 2022 22:15
Show Gist options
  • Save lekeCoder/eac22934513da63373ad62b21d6c2220 to your computer and use it in GitHub Desktop.
Save lekeCoder/eac22934513da63373ad62b21d6c2220 to your computer and use it in GitHub Desktop.
switch and when example 1
// print day of the week based on day number (1 - 7)
int day = 3;
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// prints "Wednesday"
// print day of the week based on day number (1 - 7)
val day = 3;
when(day){
1 -> println("Monday")
2 -> println("Tuesday");
3 -> println("Wednesday");
4 -> println("Thursday");
5 -> println("Friday");
6 -> println("Saturday");
7 -> println("Sunday");
}
// prints "Wednesday"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment