Switch Statement



This statement is used to check a variable for different values and according to the value of variable different code block will be executed. The switch statement causes a particular group of statement to be chosen from several available groups.

Syntax :-
  Switch (variable or expression)
    {
      Case value:
        Statement block 1;
        break;
      Case value:
        Statement block 2;
        break;
      Case value:
        Statement block 3;
        break;
      ------
      ------
      ------
      Case value:
        Statement block N;
        break;
      Default:
        default block;
    }

If no matching case is found then code in the default will be executed. Default block are optional.

Flow chart for switch statement

Switch