0% found this document useful (0 votes)
8 views

Switch Case

The document contains two Java examples demonstrating the use of switch statements. The first example prints the number corresponding to a variable, while the second example maps month numbers to their respective names and prints the name for a given month number. Both examples include case and default statements to handle various scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Switch Case

The document contains two Java examples demonstrating the use of switch statements. The first example prints the number corresponding to a variable, while the second example maps month numbers to their respective names and prints the name for a given month number. Both examples include case and default statements to handle various scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Example:

SwitchExample.java

1. public class SwitchExample


2. {
3. public static void main(String[] args)
4. {
5. //Declaring a variable for switch expression
6. int number=20;
7. //Switch expression
8. switch(number)
9. {
10. //Case statements
11. case 10: System.out.println("10");
12. break;
13. case 20: System.out.println("20");
14. break;
15. case 30: System.out.println("30");
16. break;
17. //Default case statement
18. default:System.out.println("Not in 10, 20 or 30");
19. }
20. }
21. }
Test it Now

Output:

20
Finding Month Example:

SwitchMonthExample.javaHTML

1. //Java Program to demonstrate the example of Switch statement


2. //where we are printing month name for the given number
3. public class SwitchMonthExample
4. {
5. public static void main(String[] args)
6. {
7. //Specifying month number
8. int month=7;
9. String monthString="";
10. //Switch statement
11. switch(month)
12. {
13. //case statements within the switch block
14. case 1: monthString="1 - January";
15. break;
16. case 2: monthString="2 - February";
17. break;
18. case 3: monthString="3 - March";
19. break;
20. case 4: monthString="4 - April";
21. break;
22. case 5: monthString="5 - May";
23. break;
24. case 6: monthString="6 - June";
25. break;
26. case 7: monthString="7 - July";
27. break;
28. case 8: monthString="8 - August";
29. break;
30. case 9: monthString="9 - September";
31. break;
32. case 10: monthString="10 - October";
33. break;
34. case 11: monthString="11 - November";
35. break;
36. case 12: monthString="12 - December";
37. break;
38. default:System.out.println("Invalid Month!");
39. }
40. //Printing month of the given number
41. System.out.println(monthString);
42. }
43. }
Test it Now

Output:

7 - July

You might also like