Skip to content

Commit 54c998e

Browse files
committed
feat: Compare switch-case vs. else-if ladder for number-to-word mapping
🧠 Logic: - Implemented two approaches to print number words for values 1 to 5: 1. `switch-case`: Uses a `byte` variable `n` and prints corresponding text. 2. `else-if ladder`: Uses `int n` to achieve the same logic sequentially. - Both handle default cases when the number is outside the expected range. - Useful for demonstrating syntax, readability, and performance differences between both control structures. Signed-off-by: Somesh diwan <[email protected]>
1 parent 52ad049 commit 54c998e

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//Comparison between else if Ladder and switch cases.
22

3-
public class SwitchCasesAB
4-
{
3+
public class SwitchCasesAB {
54
public static void main(String[] args) {
65
//int n = 9;
76
byte n = 9;
@@ -29,21 +28,23 @@ public static void main(String[] args) {
2928
}
3029
}
3130

32-
//public class SwitchCasesAB {
33-
// public static void main(String[] args) {
34-
// int n = 1;
35-
// if(n==1)
36-
// {
37-
// System.out.println("One");
38-
// } else if (n==2) {
39-
// System.out.println("Two");
40-
// } else if (n==3) {
41-
// System.out.println("Three");
42-
// } else if (n==4) {
43-
// System.out.println("Four");
44-
// }
45-
// else {
46-
// System.out.println("Not a valid number");
47-
// }
48-
// }
49-
//}
31+
/*
32+
public class SwitchCasesAB {
33+
public static void main(String[] args) {
34+
int n = 1;
35+
if(n==1)
36+
{
37+
System.out.println("One");
38+
} else if (n==2) {
39+
System.out.println("Two");
40+
} else if (n==3) {
41+
System.out.println("Three");
42+
} else if (n==4) {
43+
System.out.println("Four");
44+
}
45+
else {
46+
System.out.println("Not a valid number");
47+
}
48+
}
49+
}
50+
*/

0 commit comments

Comments
 (0)