Translate

Wednesday, 22 June 2016

Java Switch Construct

Hi friends ........

                          selection construct

There are two types of constructs in Java...
        1.if-else
        2.switch-case

Note: if-else you know very well in c/c++ language.In java also if-else have same concepts.

                                              switch-case

There are some points about switch........
1.switch can only have Integer or Integer expression,string,enum.
2.case should always have a constant value or literal.
3.if you not used break inside case then it also executes all the case(followed the case which matches)until the break found or end of switch reached.

Demo:

public class Switch{
 public static void main(String args[]){
  //switch
  int x=8;


  switch(x){
   case 2:
      System.out.println("yo its x=2");
      break;
   case 4:
      System.out.println("yo its x=4");
      break;
   case 6:
      System.out.println("yo its x=6");
      break;
   case 8:
      System.out.println("yo its x=8");
      break;
   default:
      System.out.println("yo its >8");

  }
 }
}

output:

yo its x=8

No comments:

Working With Java Collections