wellnwill Marathahalli Bangalore

switch statement

It is easier and shorter way to write code with switch statement which is written with nested if-else. switch statement improves the readability of the code. Syntax:

x may be < expression/value/variable >.
switch (x)   {
    case :
        // statement will be executed if x is equal to constant1;
        break;// break is optional.
    case :
        // statement will be executed if x is equal to constant2;
        break;
        ……. 
default:
        // statement will be executed if x doesn't match any constant;
}
  • The switch arguments must be byte, short, char, int up to java 1.4.
  • enum type is allowed as an argument from java 1.5.
  • String type is allowed as an argument from java 1.7.
class Test104{
  public static void main(String[] args){
  int festival = 3;

  switch (festival){
      case 1:
            System.out.println("Onam");
            break;
      case 2:
            System.out.println("Holi");
            break;
      case 3:
            System.out.println("Dusshera");
            break;
      case 4:
            System.out.println("Navratri");
            break;
      case 5:
            System.out.println("Durga Puja");
            break;
      case 6:
            System.out.println("Diwali");
            break;
      case 7:
            System.out.println("Pongal");
            break;
      default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

// O/P  -  Dusshera
class Test105 {
    public static void main(String[] args) 
    {   
        int festival=3;
        if (festival== 1) 
        {
            System.out.println("Onam");
        }
        else if (festival== 2) 
		{
            System.out.println("Holi");
        }
        else if (festival== 3) 
		{
            System.out.println("Dusshera");
        } 
        else if (festival== 4)
		{
            System.out.println("Navratri");
        } 
		else 
		{
             System.out.println("Diwali");
        }
    }
}

// O/P -  Dusshera






• Inside switch every statements must be written under case or default. • The case label should be a constant or constant expression. It should not be a variable.

class Test106
{
  public static void main(String[] args)
  {
     int a = 3;
     switch (a)
      {
      }
  }
}

// Note - compile time success. 

class Test107
{
  public static void main(String[] args)
  {
    int a = 3;
    switch (a)
    {
      System.out.println("hello");
    }
  }
}

// O/P - CompileTimeException
class Test108{
  public static void main(String[] args){
  int a = 3;
  int b= 5;
  switch (a){
      case 1:
            System.out.println("Onam");
            break;
      case b: // b must be constant/final
            System.out.println("Holi");
            break;
   
      default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

//O/P - CompileTimeException - constant expression required
class Test109
{
  public static void main(String[] args){
  int a = 3;
  final int b= 5;
  switch (a){
      case 1:
            System.out.println("Onam");
            break;
      case b:
            System.out.println("Holi");
            break;
   
      default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

// O/P - Invalid festival.
class Test110
{
  public static void main(String[] args){
  int a = 1;
  int b= 5;
   switch (a+1){
      case 2:
            System.out.println("Onam");
            break;
      case 10*2:
            System.out.println("Holi");
            break;
      default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

// O/P - Onam
class Test
{
   public static void main(String args[])
   {
      String name="RAJ";
      switch(name)
      {
       case "RAJ":
       System.out.println(name);
       break;
       case "BAJ":
       System.out.println(name);
       break;
     }
   }
}

// O/P - RAJ


• The case label should be in the range of switch argument type. • The case label should not be duplicate.

fall through in switch case

Inside switch if any case is matched from that case onwards all cases will be executed until break or end of switch. The advantage of fall through is that we can define common action for multiple cases i.e. (code reusability).

class Test111
{
  public static void main(String[] args){
  byte festival = 3;
  switch (festival)
  {
      case 1:
            System.out.println("Onam");
            break;
      case 130: // 130 is out of range for byte data type
            System.out.println("Holi");
            break;
       default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

// o/p - CompileTimeException
       - incompatible type:possile lossy conversion from int to byte.
class Test112
{
    public static void main(String[] args){
    byte festival = 3;
    switch (festival+1){
       case 1:
            System.out.println("Onam");
            break;
       case 4:
            System.out.println("Diwali");
            break;				
       case 130: 
            System.out.println("Holi");
            break;
      
       default:
            System.out.println("Invalid festival");
            break;
    }
  }
}

// o/p - Diwali.
   Note - because of arithmetic operation,switch expression value will be considered as int type.
class Test113
{
  public static void main(String[] args){
  byte festival = 1;
  switch (festival)
  {
      case 1:
            System.out.println("Onam");
      case 4:
            System.out.println("Diwali");
      case 5: 
            System.out.println("Holi");
            break;
      default:
            System.out.println("Invalid festival");
            break;
      }
  }
}

// O/P - Onam,Diwali,Holi
class Test114
{
  public static void main(String[] args){
  byte a = 1;
  switch (a)
  {
      case 1:
            System.out.println("case1");
      case 1:
            System.out.println("case1");  
  } 
}

// O/P - CompileTimeException - duplicate case label







class Test115
{
    public static void main(String[] args)
	{
      byte festival = 1;
      switch (festival)
      {
         default:
             System.out.println("Invalid festival");
         case 1:
            System.out.println("Onam");
            break;
         case 2: 
            System.out.println("Holi");
       }
    }
}

// O/P - Onam


class Test114
{
  public static void main(String[] args){
  byte a = 1;
  switch (a)
  {
      case 1:
            System.out.println("case1");
      case 1:
            System.out.println("case1");  
  } 
}

// O/P - CompileTimeException - duplicate case label







class Test115
{
    public static void main(String[] args)
	{
      byte festival = 1;
      switch (festival)
      {
         default:
             System.out.println("Invalid festival");
         case 1:
            System.out.println("Onam");
            break;
         case 2: 
            System.out.println("Holi");
       }
    }
}

// O/P - Onam
class Test116
{
   public static void main(String[] args)
   {
     byte festival = 2;
     switch (festival)
     {
       default:
             System.out.println("Invalid festival");
       case 1:
            System.out.println("Onam");
            break;
       case 2: 
            System.out.println("Holi");
     }
  }
}

// O/P - Holi
Footer with Map

Top