Thursday, March 31, 2016

Switch Statements

Welcome back guys, this time we're gonna learn about Switch Statements.

You may wonder what're these switch statements.. Simply "Multiple IF conditions". 

Let's get ordinary IF condition

         if(x == 0){
System.out.println("Small");
} else if(x== 1){
System.out.println("Medium");
} else if(x == 2){
System.out.println("large");
}else
System.out.println("extra large");

}
You see when comes to a big conditional coding, over and over you have to type if conditions. But using switch statements it's gonna be easy. Take this example and try your own idea.

int x = 3;

switch (x){

case 1: 
System.out.println("Small");
break;
case 2:
System.out.println("Medium");
break;
case 3:
System.out.println("Large");
break;
case 4:
System.out.println("Very Large");
break;
case 5:
System.out.println("Extra Large");
break;
default:
System.out.println("Normal");
break;

}



  1. Always remember to put "break;" to finish the condition. Unless all the conditions will apply.
  2. "default :" should be there, it act as "else". 
That's all...





1 comment: