Switch Case Statement in C Language

 Switch Case Statement in C Language


What is switch case statement in C?

The switch case statement in C language allows us to execute one code block among the various alternative code blocks. Programmer can execute the code block as per his choice. The specific case or option given by the programmer is executed by the compiler among all the cases. We can use the if else ladder statement to perform multiple tasks but switch case statements are easier than other conditional statements.   It is used to perform different actions based on different conditions. It is a multiway branch statement and provides an easy way to execute different parts of code based on the value of the expressions. 

The switch keyword is used to define cases of users. In switch statement case values can be int or char. 

Following are the rules to use the switch case statement 

1. You can write one or N number of cases.

2. The value in case must be unique

3. Each case statement have break statement to transfer the control of program into next block.

4. Duplicate case values are not allowed.


Syntax of switch case:


switch(expression)

{

Case value1:

Statement1;

Break;

Case value2:

statement2;

Break;

Case value3:

Statement3;

Break;

Case valueN:

statementN;

Break;

Default:

Default_statement;

}


Break keyword is used to stop the execution inside the switch block. It helps to terminate the switch block and break out of it.

Default keyword is used to execute the set of statements if no case option is found or no case match found. Default statement is optional. 


Following program demonstrate the use of switch case statement


/***************************************************************

 *C Program to perform arithmetic operation using switch 

 ****************************************************************/

 #include<stdio.h>

 void main()

 {

     int a,b,result,choice;

      printf("Enter two numbers\n");

     scanf("%d%d",&a,&b);

      printf(" Choice 1 : Addition\n");

     printf("Choice 2: Subtraction\n");

     printf("Choice 3: Multiplication\n");

     printf("Choice 4: Division\n");

     printf("Choice 5: Remainder\n");

     printf("Enter your choice\n");

     scanf("%d",&choice);

     switch(choice)

     {

         case 1: 

         result=a+b;

         printf("The addition is:%d\n",result);

         break;

         case 2:

         result=a-b;

         printf("The subtraction is:%d\n",result);

         break;

         case 3:

         result=a*b;

         printf("The multiplication is:%d\n",result);

         break;

         case 4:

         result= a/b;

         printf("The division is:%d\n",result);

         break;

         case 5:

         result =a%b;

         printf("The remainder of division is: %d\n",result);

         break;

         default:

         printf("Invalid input, Please input valid choice\n");

         break;

     }

 }


Enter two numbers

60

40

 Choice 1 : Addition

Choice 2: Subtraction

Choice 3: Multiplication

Choice 4: Division

Choice 5: Remainder

Enter your choice

5

The remainder of division is: 20


टिप्पणी पोस्ट करा

0 टिप्पण्या