Created
April 28, 2022 20:54
-
-
Save lekeCoder/6e7719379c35512d32df45cc5c636d53 to your computer and use it in GitHub Desktop.
switch vs when example 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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