Skip to content

Commit 47031ce

Browse files
committed
feat: Add nested switch examples for employee details using traditional and enhanced syntax
🔍 Details: - Implemented two versions demonstrating nested switch statements. - `NestedSwitch`: Uses both traditional and enhanced switch formats to print employee name and department. - `NestedSwitch2`: Adds user prompts, newline handling, and clearer output labeling for better UX. - Handles nested switch logic for employee ID 3 with department-specific outputs (IT/Management). 🧠 Purpose: - Practice and compare traditional vs. enhanced switch syntax (Java 14+). - Illustrate structured decision-making with nested conditions. Signed-off-by: Somesh diwan <[email protected]>
1 parent 30a0607 commit 47031ce

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Section7ConditionalStatements/Practice/src/NestedSwitch.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class NestedSwitch {
44
public static void main(String[] args) {
55
Scanner in = new Scanner(System.in);
6+
67
int empID = in.nextInt();
78
String department = in.next();
89

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Scanner;
2+
3+
public class NestedSwitch2 {
4+
public static void main(String[] args) {
5+
Scanner in = new Scanner(System.in);
6+
7+
System.out.print("Enter Employee ID: ");
8+
int empID = in.nextInt();
9+
in.nextLine(); // Consume newline
10+
System.out.print("Enter Department (if applicable): ");
11+
String department = in.nextLine();
12+
13+
// Traditional switch statement
14+
System.out.println("\n--- Traditional Switch Output ---");
15+
switch (empID) {
16+
case 1:
17+
System.out.println("Kunal Kushwaha");
18+
break;
19+
case 2:
20+
System.out.println("Rahul Rana");
21+
break;
22+
case 3:
23+
System.out.println("Emp Number 3");
24+
switch (department) {
25+
case "IT":
26+
System.out.println("IT Department");
27+
break;
28+
case "Management":
29+
System.out.println("Management Department");
30+
break;
31+
default:
32+
System.out.println("No valid department entered");
33+
}
34+
break;
35+
default:
36+
System.out.println("Enter correct EmpID");
37+
}
38+
39+
// Enhanced switch expression (Java 14+)
40+
System.out.println("\n--- Enhanced Switch Output ---");
41+
switch (empID) {
42+
case 1 -> System.out.println("Kunal Kushwaha");
43+
case 2 -> System.out.println("Rahul Rana");
44+
case 3 -> {
45+
System.out.println("Emp Number 3");
46+
switch (department) {
47+
case "IT" -> System.out.println("IT Department");
48+
case "Management" -> System.out.println("Management Department");
49+
default -> System.out.println("No valid department entered");
50+
}
51+
}
52+
default -> System.out.println("Enter correct EmpID");
53+
}
54+
55+
in.close();
56+
}
57+
}

0 commit comments

Comments
 (0)