Image

C Language - Control Statements - Switch Case Statement

Switch Case Statement

When we want to execute the multiple statement at that time we used switch case statement . It is like a else if ladder, but suppose in else if ladder there are five statement and our fifth statement is executed then compiler start the checking execution from first statement but in switch case there is no need to check each and every condition it will jump on particular statement .

Syntax:  switch(expression)
	    {
  	          case1: statement  1;
		break;
	          case 2: statement  2;
		break;
	          case 3: statement 3;
		break;
		|
  	        |
	          case 4: statement N;
		break;
	          default:  default statement; 	
    }
The default statement is executed while no statement is executed.

Rules for switch case
1) The switch expression must be integer and character type.
2) The case value should be character or integer constant.
3) The floting point value does not allowed in expression.

EX:
 	#include<stdio.h>
	#include<conio.h>
	void main()
	{
             int choice;
	     printf(“Enter your choice”);
	     scanf(“%d” &choice);
		switch(choice)
		{
		Case 1:
			printf(“ First \n”);
			break; //optional
		case 2:
			printf(“Second \n”);
			break;
		case 3:
			printf(“Three \n”);
			break;
		default:
			printf(“Wrong choice \n”);
		}
	getch();
	}
o/p
  Enter your choice     2
     ans:  Second