• Spring
  • jQuery
  • JavaScript
  • RubyonRails
  • Servlet
  • SQL Tutorial
  • Struts
  • Core Java
  • JSP

  • <

    Java Switch Case Tutorial

    It execute different parts of code an easy way based on the value of the expression.Switch case values must be unique.


    Switch case syntax:
    
    switch(expression)
    {
       // case statements
       // values must be of same type of expression
       case value1 :
          // Statements
          break; // break is optional
       
       case value2 :
          // Statements
          break; // break is optional
       
       // We can have any number of case statements
       // below is default statement,used when none of the cases is true. 
       // No break is needed in the default case.
       default : 
          // Statements
    }
    

    Java Switch Case Example


    
    public class SwitchDemo { 
        public static void main(String[] args) { 
    
            int month =10; 
            String monthString; 
            switch (month) { 
                case 1:  monthString = "January"; 
                         break; 
                case 2:  monthString = "February"; 
                         break; 
                case 3:  monthString = "March"; 
                         break; 
                case 4:  monthString = "April"; 
                         break; 
                case 5:  monthString = "May"; 
                         break; 
                case 6:  monthString = "June"; 
                         break; 
                case 7:  monthString = "July"; 
                         break; 
                case 8:  monthString = "August"; 
                         break; 
                case 9:  monthString = "September"; 
                         break; 
                case 10: monthString = "October"; 
                         break; 
                case 11: monthString = "November"; 
                         break; 
                case 12: monthString = "December"; 
                         break; 
                default: monthString = "Invalid month"; 
                         break; 
            } 
            System.out.println(monthString); 
        } 
    } 
    

    OUTPUT:
    October
    














    © copyright 2017-2022 Completedone pvt ltd.