Using switch Structures

The following is a Java Program using switch structures. In Java, switch, case, break and default are reserved words. In a switch structure, the expression is evaluated first. The value of expression is then used to perform the actions specified in the statements that follow the reserved word case. I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code:
  1. import java.util.*;
  2. public class BreakStatementInSwitch
  3. {
  4. static Scanner console = new Scanner(System.in);
  5.  
  6. public static void main (String[] args)
  7. {
  8. int num;
  9.  
  10. System.out.println("Enter an integer between 0 and 10: ");
  11.  
  12. num = console.nextInt();
  13. System.out.println();
  14.  
  15. System.out.println("The number you enter is " + num);
  16.  
  17. switch (num)
  18. {
  19. case 0:
  20. case 1: System.out.print("Hello " );
  21. case 2: System.out.print("there " );
  22. case 3: System.out.print("I am " );
  23. case 4: System.out.println("Dodgers " );
  24. break;
  25. case 5: System.out.print("How " );
  26. case 6:
  27. case 7:
  28. case 8: System.out.print("are you " );
  29. break;
  30. case 9: break;
  31. case 10: System.out.println("Have a nice day!" );
  32. break;
  33. default: System.out.println("Sorry the number is out of range!");
  34.  
  35.  
  36. }
  37. System.out.println("Out of the switch structure!");
  38.  
  39. }
  40. }
  1. Sample Run 1:
  2. Enter an integer between 0 and 10:
  3. 1
  4. The number you enter is 1
  5. Hello there I am Dodgers
  6. Out of the switch structure!
  7.  
  8. Sample run 2:
  9. Enter an integer between 0 and 10:
  10. 3
  11.  
  12. The number you enter is 3
  13. I am Dodgers
  14. Out of the switch structure!
  15.  
  16. Sample run 3:
  17.  
  18. Enter an integer between 0 and 10:
  19. 4
  20.  
  21. The number you enter is 4
  22. Dodgers
  23. Out of the switch structure!
  24.  
  25. Sample run 4:
  26.  
  27. Enter an integer between 0 and 10:
  28. 5
  29.  
  30. The number you enter is 5
  31. How are you Out of the switch structure!
  32.  
  33. Sample run 5:
  34.  
  35. Enter an integer between 0 and 10:
  36. 7
  37.  
  38. The number you enter is 7
  39. are you Out of the switch structure!
  40.  
  41. Sample run 6:
  42.  
  43. Enter an integer between 0 and 10:
  44. 9
  45.  
  46. The number you enter is 9
  47. Out of the switch structure!
  48.  
  49. Sample run 7:
  50.  
  51. Enter an integer between 0 and 10:
  52. 10
  53.  
  54. The number you enter is 10
  55. Have a nice day!
  56. Out of the switch structure!
  57.  
  58. Sample run 8:
  59.  
  60. Enter an integer between 0 and 10:
  61. 11
  62.  
  63. The number you enter is 11
  64. Sorry the number is out of range!
  65. Out of the switch structure!
A walk through of this program, using certain values of the switch expression, num, can help you understand how the break statement functions. If the value of num = 0, the value of the switch expression matches the case value . The first break appears  break;, just before the case value of 5. When the value of the switch statement expression matches a case value, all statements execute until a break is encountered, and the program skips all case labels in between. If the value of num is 3, it matches the case value of 3 and the statements following this label execute until the break statement is encountered in  break;. If the value of num is 9, it matches the case value of 9. In this situation, the action is empty, because only break statement case 9: break;, follows the case value of 9.

Add new comment