Switch Statement Java

  • Post author:
Download PDF
Advertisement

Switch Statement Java

The switch statement Java programming consider as multiway branch statement. It is another conditional structure of programming techniques. It is a good alternative of nested if-else. It can be used easily when there are many choices available and only one should be executed. Nested if becomes very difficult in such situation therefore switch statement used here.

Syntax of switch statement:

   switch (expression) 
   {
      case value1:
      // statement of value 1 case
      break;
      case value2:
      // statement of value 2 case
      break;
      case valueN:
      // statement of value N case
      break;
      default:
      // default statement sequence
   }

The expression part of the switch must be of type byte, short, int, or char. Each values which specified in the case portion must be a type of same with the expression part. Value of case must be a unique literal. This literal must be a constant, not a variable. Duplication in case values are not allowed. Multiple case values can be used for one single block.

Example:

   case ‘a’;
   case ‘A’;
   System.out.println(“Hello Multiple case: ”);
   break;

Each switch statement used one default value which is executed when above all case values not matched with expression variable. The break statement is used in switch to terminate flow of the sequence.

When one break statement is executed then compiler skip all remaining case value portions and jump out to the outside the switch statement. If all one case values not matched with the expression variable value then default portion of the switch is executed and terminate the switch statement. The default statement is optional. If no any case value matches or default

Advertisement
is also not present, then no further action is taken by compiler.

Flowchart of the switch statement:

switch statement java

Below is a sample program which demonstrate the switch statement.

   class ZitocSwitch {
      public static void main(String args[]) {
      int zi;
      for(zi=0; zi<6; zi++)
      {
         switch(zi)
         {
            case 0:
            System.out.println("variable zi is zero.");
            break;
            case 1:
            System.out.println("variable zi is one.");
            break;
            case 2:
            System.out.println("variable zi is two.");
            break;
            case 3:
            System.out.println("variable zi is three.");
            break;
            default:
            System.out.println("variable zi is greater than 3.");
         }
      }
    }
   }

Output:

zi is zero.

zi is one.

zi is two.

zi is three.

zi is greater than 3.

zi is greater than 3.

Here is another example program for switch statement:

Write a Java program that inputs number of week’s day and displays the name of the day. For example if user enter 1 it display Monday and so on.

   import java.util.Scanner;

   class ZitocSwitch {
   public static void main(String args[]) {
   // Create a Scanner object
   Scanner input = new Scanner(System.in);
   // For Enter Value
   System.out.print("Enter a number for weekday: ");
   int no = input.nextInt();
   switch(no)
   {
	case 1:
	System.out.println(“Monday”);
	break;
	case 2:
	System.out.println(“Tuesday”);
	break;
	case 3:
	System.out.println(“Wednessday”);
	break;
	case 4:
	System.out.println(“Thursday”);
	break;
	case 5:
	System.out.println(“Friday”);
	break;
	case 6:
	System.out.println(“Saturday”);
	break;
	case 7:
	System.out.println(“Sunday”);
	break;
	default:
	System.out.println(“You put wrong number for week days:”);
     } // switch body end
   } // main method body end
   } // class ZitocSwitch body end

 

If-else java statements

Advertisement