Skip to content

Instantly share code, notes, and snippets.

@lekeCoder
Created April 28, 2022 20:54
Show Gist options
  • Save lekeCoder/6e7719379c35512d32df45cc5c636d53 to your computer and use it in GitHub Desktop.
Save lekeCoder/6e7719379c35512d32df45cc5c636d53 to your computer and use it in GitHub Desktop.
switch vs when example 2
// combined case blocks support
// print whether a day is a working day or weekends based on day number (1 - 7)
int day = 3;
switch(day){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("Work day");
break;
case 6:
case 7:
System.out.println("Weekend");
break;
}
// prints "Work day"
// combined case blocks support
// print whether a day is a working day or weekends based on day number (1 - 7)
val day = 3;
when(day){
1, 2, 3, 4, 5 -> print("Work day")
6, 7 -> print("Weekend");
}
// prints "Work day"
// so concise !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment