Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Monday, November 23, 2020

switch case statement in C

switch case statement: is used when we have multiple options and we need to perform a different task for each option.

switch-case statement | welcome2protec.com

The switch case statement is used when we have multiple options (choice) and we need to perform a different task for each option. You can do the same thing with the If...else...if ladder.


Syntax:

switch(expression){    
 case constant1
  //code to be executed;    
 break;  //optional  
 case constant2
  //code to be executed;    
 break;  //optional  
 ......    
    
 default:     
  code to be executed if all cases are not matched;    
}    

How does the switch statesmen work?

The expression is evaluated once and compared with the values of each case label.

  • If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant 2, statements after case constant 2 are executed until break is encountered.
  • If there is no match, the default statements are executed.

Flow Chart of switch...case statement:

switch-case statement | welcome2protec.com
switch-case statement | welcome2protec.com

Lets take a simple example to understand the working of switch...case statement in c.

Example 1: switch...case in C:

#include <sdtio.h> 
int main(){    
  int number=0;     
  printf("enter a number:");    
  scanf("%d", &number);    
  switch(number){    
   	 case 10:    
 		printf("number is equals to 10");    
 		break;    
 	 case 20:    
 		printf("number is equal to 20");    
 		break;    
   	 case 30:    
  		printf("number is equal to 30");    
 	 	break;    
 	 default:    
 		printf("number is not equal to 10, 20 or 30");    
    }    
    return 0;  
 }

Output

Output 1:
 enter a number:4
 number is not equal to 10, 20 or 30

Output 2:
 enter a number: 20
 number is equal to 20

There is a default statement in switch...case which is optional. If switch expression does not match with any case, default statement are executed by the program. As you can see in output 1.


Example 2: Switch...case

#include <stdio.h>   
int main () { 
 
   /* local variable definition */ 
   char grade = 'B'; 
 
   switch(grade) { 
      case 'A' : 
         printf("Excellent!\n" ); 
         break; 
      case 'B' : 
      case 'C' : 
         printf("Well done\n" ); 
         break; 
      case 'D' : 
         printf("You passed\n" ); 
         break; 
       
      default : 
         printf("Invalid grade\n" ); 
   } 
   printf("Your grade is  %c\n", grade ); 
   return 0; 
} 

When the above code is complied and executed, it produces the following result.

Output

Well done
You grade is B

You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.

The default statement is executed if no case is equal to the value of switch ( expression ). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. A case or default label can only appear inside a switch statement.


Example 3: Switch case

In absence of break, all cases below the intended case gets executed one-by-one. Lets take an example to understand it better.

#include <stdio.h>
int main()
{
  int x = 0;
  switch(x) 
  { 
	case 0 : printf("\n Its a zero"); 
 
	case 1 : printf("\n Its a one"); 
 
	default : printf("\n Its something else"); 
  }
  return 0;
}

Output

INPUT : x = 0 
	Its a zero 
	Its a one 
	Its something else

In above code, if I didn’t use the break statements, then all the cases below the correct case ( case 0) are executed unintentionally. To prevent this ‘break statements’ are used. See below example.


Example 4: Switch case

#include <stdio.h>
int main()
{
  int x = 0;  
  switch(key) 
  { 
	case 0 : printf("\n Its a zero"); 
			 break; 
 
	case 1 : printf("\n Its a one"); 
			 break; 
 
	default : printf("\n Its something else"); 
			  break; 
  }
  return 0;
}

Output

INPUT : x = 0  
It's a zero 







Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us