Tuesday 27 May 2014

Switch In Java

The switch statement in  Java is used to execute the code from multiple conditions. It provides an easy way to dispatch execution to different parts of code based on the value of expression.It is a better alternative than a large series of if-else-if statements.

The syntax of switch statement :

switch(expression)
{
      case value1:
              //statements
        break;
     case value2:
              //statements
        break;
       .
       .
       .
       .
    case valueN:
              //statements
        break;
    default:
        //default statement
}
Flow Chart of switch Statement:



Rules for switch statement in Java language

1.  The switch expression must be of  byte,short,int or char type.
2.  The case value must be of type compatible with the expression.
3. Each case value must be a unique literal (means it must be a constant , not a variable).
4. Duplicate case values are not allowed.

How switch statement works ?
The value of the expression is compared with each of the literal values in the case statement.If match is found ,the code sequence following that case statement is executed.If none of the constants matches the value of the expression then the default statement is executed.Default statement is optional.
The break statement inside the switch is used for terminate a statement sequence.When a break statement is encountered , the execution comes out of the switch block to first line after the switch block.
If we are not keeping the break statement then , if  any case matched then all the case after that case will execute.

Simple Switch statement program

package org.modi;
import java.util.Scanner;
public class Test
{
public static void main(String[] args) 
{
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your choice");
i=sc.nextInt();
switch(i)
{
case 0:
System.out.println("This is 0");
break;
case 1:
System.out.println("This is 1");
break;
case 2:
System.out.println("This is 2");
break;
case 3:
System.out.println("This is 3");
break;
case 4:
System.out.println("This is 4");
break;
default:
System.out.println("Default statement");
}
}
}
OUTPUT

Enter your choice                  //1st time execution
1
This is 1
Enter your choice              //2nd time execution
8
Default statement

Switch statement without break statement:
package org.modi;
import java.util.Scanner;
public class Test
{
 public static void main(String[] args) 
 {
  int i;
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter your choice");
  i=sc.nextInt();
  switch(i)
  {
   case 0:
    System.out.println("This is 0");
   
   case 1:
    System.out.println("This is 1");
    
   case 2:
    System.out.println("This is 2");
    
   case 3:
    System.out.println("This is 3");
    break;
   case 4:
    System.out.println("This is 4");
    break;
   default:
    System.out.println("Default statement");
  
  }
  
 }
}

OUTPUT
Enter your choice
0
This is 0
This is 1
This is 2
This is 3


0 comments :

Post a Comment