-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedSwitch.java
More file actions
66 lines (53 loc) · 2.28 KB
/
NestedSwitch.java
File metadata and controls
66 lines (53 loc) · 2.28 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class NestedSwitch {
public static void main(String[] args) {
//Nested Switch
int year = 1;
String dept ="ECE";
switch(year){
case 1:
System.out.println("The Code Bird");
break;
case 2:
switch(dept){
case "CSE":
case "IT" :
System.out.println("The Coding Junction");
break;
case "ECE":
System.out.println("KM community");
break;
}
}
//Enum or Enumerators Or Constants Using Switch Cases
enum Dept { CSE, IT ,ECE, CIVIL, AEIE, EE} //defines a set of constant values
Dept DEP = Dept.CSE; //defining an element DEP from Dept-set using '.' 'element'
switch (DEP){ //running switch-case for the defined element
case CSE -> System.out.println("He is from CSE branch");
case IT -> System.out.println("he is form IT branch");
case ECE -> System.out.println("he is form ECE branch");
case CIVIL -> System.out.println("he is form CIVIL branch");
}
//switch as an Expression(variable)
int likes = 100;
String meme = "Hello and Welcome to my GUYS!";
String result = "ei baba";
result = switch(likes){ // switch is used as an Assigned value for result
case 50 -> "i wont post again "+ meme;
case 100 -> "i will post again "+ meme;
case 200 -> "i will post daily "+ meme;
case 1000 -> "i am an Influencer now "+ meme;
default -> "Stop Using INSTAGRAM... "+ meme;
};
System.out.println(result);
// Using yeild Keyword insted of Lambda Expression '->'
String Students = "";
String results ="";
results = switch(Students){ // Switch has to be used as an Expression, like results in this case
case "a" : yield "i wont post again"; // yield is used
case "b" : yield "i will post again";
case "c" : yield "i will post daily ";
case "d" : yield "i am an Influencer now ";
default : yield "Stop Using INSTAGRAM... "; // default statement is a must
};
}
}