switch condition

The switch statement is used to pick up or execute a particular group of statements from several groups of the statement. It allows us to make a decision from the number of choices. It is a multiway decision statement, it tests the value of a given variable or expression against a list of case values and when a match then block of statements will be executed. The expressions in the switch statement must be either an integer or character constant. Each case and default case block will be terminated with break statements. Then, the default case block should be terminated at the end of your cases.

Syntax
              
   switch(expression)
   {
       case 1:
       statement 1;
        break;
    case 2:
        statement 2;
        break;
   case 3 :
        statement 3;
        break;
.
.
.
   case n:
     statement n;
     break;
  default:
       statement;
       break;
}
  
flow chart:
   


 this the flow chart for switch case.

program:

 #include<stdio.h>
 #include<conio.h>

 void main()
 {
   
int a=5,b=5,c,choice;
   
printf("1.add\n 2.sub\n 3.multiply\n);
   
printf("enter the choice\n");
   
scanf("%d", &choice);
   
switch(choice)
  {
    case 1:
           c=a+b;
         
 printf("addition is:%d",c);
           
break;
   
case 2:
          c=a-b;
       
  printf("subtraction is:%d",c);
         
break;
 
 case 3:
         c=a*b;
         printf("multiply  is:%d",c);
     
   break;
 
default:
       
printf("your choice is not there in our menu .so choice correct choice on our menu\n");
   

 }
   
getch();
 }

 

output:

 

output:


1.add
2.sub
3.multiply
enter your choice:

1
addition is :10

see this output. In this program, we are finding the addition, subtraction, multiplication of all the process in one program by using a switch case. Now we see how it will execute, first, we got one menu that we have written in our program. then, we have to give which option we are going to proceed with, the next step it will go to witch case what choice we have given by the user, then our compiler going to find where is that our given choice and as well as what operation they are given in this program everything our compiler will identify then it will proceed that corresponding operation, the last step it will give our output.

No comments:

Post a Comment