Switch Statement Example

  1. /*
  2. Switch Statement Example
  3. This example shows how to use switch statement in a Java program.
  4. Switch statement is a better replacement if multiple if else if
  5. statements.
  6. */
  7.  
  8. public class SwitchStatementExample {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. /*
  13. * Syntax of switch statement is
  14. *
  15. * switch(expression){
  16. *
  17. * case value1:
  18. * //statements
  19. * break;
  20. *
  21. * case value2:
  22. * //statements
  23. * break;
  24. *
  25. * ....
  26. *
  27. * default:
  28. * //statements
  29. * break;
  30. *
  31. * }
  32. *
  33. * here, expression must be of type int, short, byte or char.
  34. * values should be constants literal values and can not be
  35. * duplicated.
  36. *
  37. * Flow of switch statement is as below.
  38. * Expression value is compared with each case value. If it
  39. * matches, statements following case would be executed.
  40. * break statement is used to terminate the execution of
  41. * statements.
  42. *
  43. * If none of the case matches, statements following default
  44. * would be executed.
  45. *
  46. * If break statement is not used within case, all cases following
  47. * matching cases would be executed.
  48. *
  49. */
  50.  
  51. for(int i=0; i <= 3 ; i++)
  52. {
  53. switch(i)
  54. {
  55. case 0:
  56. System.out.println("i is 0");
  57. break;
  58.  
  59. case 1:
  60. System.out.println("i is 1");
  61. break;
  62.  
  63. case 2:
  64. System.out.println("i is 2");
  65. break;
  66.  
  67. default:
  68. System.out.println("i is grater than 2");
  69.  
  70. }
  71. }
  72. }
  73. }
  74.  
  75. /*
  76. Output would be
  77. i is 0
  78. i is 1
  79. i is 2
  80. i is grater than 2
  81. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!