switch case in C Programming

In this tutorial, You will learn about the switch case statement in C programming and create c program with example.

Switch Case

When we want to solve multiple option problem and we have only one expression to evaluated different cases from it. The result decides which condition is invoked from the case of expression.
the switch statement allows us to choose only one case at the time. 

The selection of cases depends on the resultant value of test expression tested for equality against a list of integers or character constant. When the test expression result match is found with that constants then statements are executed.  

Syntax of switch case

The syntax of switch case statement as follows 


switch(test expression) {

case constant1: sequence of statements1;
		break;

case constant2: sequence of statements2;
		break;
.
.
.
case constantN: sequence of statementsN;
		  break;

default: sequence of statements N; // Optional
}

switch case Flowchart

switch case in C Programming

Rules for switch statement

  1. case labels must be unique.
  2. case label ends with a colon(:).
  3. switch case has atmost one default statement.
  4. The default statement can be placed anywhere inside the switch.
  5. Nesting of switch inside the switch is allowed but it generates more complexity in the program. However, it should be avoided.  

How does the switch statement work

  1. You have to pass the test expression or constant value inside the argument section as mention in the above syntax.
  2. The test expression is evaluated and its result value is compared with the values of constants specified in different cases statement.
  3. When the match found it execute that sequence of statements until the break statement or reach to switch statement ends.
  4. If the case does not have any break statement in it then continue to the next case statement until another break statement or switch statement ends.
  5. If the match is not found in any cases then it gets to execute the default statement which is optional.
  6. If the default is not present then it fails all compare and no activities take place. 

Example

#include<stdio.h>
int main(){	
	int a,b,choice;
	float result;
	do
	{	
	 printf("\nMenu");
	 printf("\n1. ADDITION");
	 printf("\n2. SUBTRACTION");
	 printf("\n3. MULTIPLICATION");
	 printf("\n4. DIVISION");
	 printf("\n5. EXIT");
	 printf("\nEnter Your Choice");
	 scanf("%d",&choice);
	 printf("\n Enter two numbers :- \t");
	 scanf("%d%d",&a,&b);
	   switch(choice) {		
	     case 1: result=a+b;
		     printf("\nAddition of %d and %d is %f",a,b,result);
		     break;
	     case 2: result=a-b;
		     printf("\nSubtraction of %d and %d is %f",a,b,result);
		     break;
             case 3: result=a*b;
		     printf("\nMultiplication of %d and %d is %f",a,b,result);
		     break;
	     case 4: result=a/b;
		     printf("\nDivision of %d and %d is %f",a,b,result);
		     break;
	     case 5: exit(1);
             default: printf("\nInvalid Choice\nPlease enter between 1 to 5");
	    }
	} while(1);
  return 0;
}

OUTPUT

switch case in C Programming with Example

In this tutorial, you learn switch case statement in C programming. Also, create c program with the help of an example. In the upcoming tutorial, I will come with difference with if else and switch case statement. Read C if else and then try to comment difference between both.

Happy Coding ๐Ÿ˜Š

No comments:

If you have any doubts, Please let me know

Powered by Blogger.