-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnhancedSwitchStatement.java
48 lines (43 loc) · 1.59 KB
/
EnhancedSwitchStatement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class EnhancedSwitchStatement {
public static void main(String[] args) {
/*
This code is using the ENHANCED switch statement instead of the switch statement.
*/
int switchValue = 3;
switch (switchValue) {
case 1 -> System.out.println("Value was 1");
case 2 -> System.out.println("Value was 2");
// You can group many cases together if you want to test many values.
// case 3: case 4: case 5:
case 3, 4, 5 -> {
System.out.println("Was a 3, a 4, or a 5");
System.out.println("Actually it was a " + switchValue);
}
default -> System.out.println("Was not 1, 2, 3, 4, or 5");
}
/*
The code below is written using the switch statement INSTEAD of the enhanced switch statement.
*/
// int switchValue = 3;
//
// switch (switchValue){
// case 1:
// System.out.println("Value was 1");
// break;
// case 2:
// System.out.println("Value was 2");
// break;
//// You can group many cases together if you want to test many values.
//// case 3: case 4: case 5:
// case 3:
// case 4:
// case 5:
// System.out.println("Was a 3, a 4, or a 5");
// System.out.println("Actually it was a " + switchValue);
// break;
// default:
// System.out.println("Was not 1, 2, 3, 4, or 5");
// break;
// }
}
}