Fallowing icon

Tuesday, September 25, 2012

Java Switch case Statements



          The switch statement is liked that if statement. We can say it's shorthand for a certain kind of if statement and those are the control statements. Some times you want to use many if statement in a program, But it's not like good. You can see this below example.
 
    Example-
           
                    if(Expression1) {
                            Statement1;
                            .................;
                    }
                    else if(Expression2){
                            Statement1;
                            .................;
                    }
                    else if(Expression2){
                            Statement1;
                            .................;
                    }
                    else if(Expression2){
                            Statement1;
                            .................;
                    }
                     ............................
                     ............................
                    else
                            Statement1;


                         
 In this case, We use switch case statements
      The switch statements are allowed to use byte,short,int,char and String data types. Cannot be use float,double or any object type data type without String
Important- 
      String data type is only allowed java 7 update or up to this only Java compiler give error massage to use String data type in switch statement, update your Java software. Get latest Java Version

The structure of the switch statement is this-


             switch(Expression){
             
             case1:
                       statement1;
                       break;
             case2:
                       statement2;
                       break;
             case3:
                       statement3;
                       break;
               ..........................
               .......................... 
             default:
                       statement4;
                       break;                  
             }



There are four main keywords in switch case statements. There are switch,case,break and default.


 switch
              You start this statement the word switch. The value you want to check insert the round brackets in front of the switch statement.


case
              After you can see the word case. Now you inset the value is you want to compare the switch value in front of the word case


break
              The word break statement terminates the enclosing switch statement. When you missed use break statement under the case block, program throw next case statement execute again and again when catch the break; statement. So don't miss use it.


default
              Finally you can see the word default. When the all case values are not matched with the switch expression, then execute the default statement. 



Example 1-


       public class Test { 
    
                   public static void main(String args[]){
    
                               int myAge=25;                                  //declare the variable myAge and assign to 25
       
                              switch(myAge){                                  //declare the switch statement
                                      case 1:
                                              System.out.println("I am a toddler");
                                              break;
                                      case 6:
                                              System.out.println("I am a student");
                                               break;
                                      case 15:
                                               System.out.println("I am a teenager");
                                               break;
                                      case 25:
                                                System.out.println("I am a younger");
                                                break;
                                      case 80:
                                                System.out.println("I am a elder");
                                                break;
                                      default:
                                                System.out.println("ayyy");
                                                break;
                                }
                   }    
      }





//Output is I am a younger

* Copy this code and past on Note pad. Use Test.java for file name and save it.


Discussion-
       
        The myAge variable is created and assign the value of 25 then give this variable for the switch expression. 

        First check the expression is equal to 1 in the case statement. But it's not equal. then check switch value is equal to 6. But it's not equal. After check switch value is equal to 15, It's not equal to this value. Now check switch value is equal to 25. In this time it's equal. So execute case statement then throw switch statement. 



Example 2-


   public class Test { 
    
      public static void main(String args[]){
    
            String luckyDay="Mondy";                                  //declare the variable luckyDay and assign to Monday
       
            switch(myAge){                                  //declare the switch statement
              case "Sunday":
                  System.out.println("My lucky day is  "+luckyDay);
                  break;
              case "Monday":
                  System.out.println("My lucky day is   "+luckyDay );
                  break;
              case "Tuesday":
                  System.out.println("My lucky day is   "+luckyDay );
                  break;
              case "Wednesday":
                  System.out.println("My lucky day is   "+luckyDay );
                  break;
              case "Friday":
                   System.out.println("My lucky day is   "+luckyDay );
                   break;
              default:
                   System.out.println("My lucky day is   "+luckyDay );
                   break;
              }      
        }    
    }





//Output is My


* Copy this code and past on Note pad. Use Test.java for file name and save it.


   String data type is used for switch value. After program check case statement one by one like example one.    
         

No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment