Switch Statements (features, disadvantages and difference with if else)

In this tutorial of the switch statement, we are going to learn what switch statement is, when it is used, what are the features and disadvantages of it? How switch statement is different from if else statement?
Submitted by Mansha Lamba, on September 05, 2018

There are loads of conditional statements available in the programming world. Each one of them has its own advantages and disadvantages. In this article, we will be discussing Switch statements.

switch : A compound statement

This statement is used when we have to select one choice from multiple choices on the basis of the value of some variable.

Syntax for switch statement:

    switch(expression/condition)
    {
	    case 1:
		    statements;
		    [break;]
	    case 2:
		    statements;
		    [break;]
	    default:
		    statements;
    }

Here, expression can be anything that returns integer value or character value but not a floating value.

Example:

#include <stdio.h>

int main()
{
	int a = 2 ;

	switch(a)
	{
		case 1:
			printf("abc");
			break;
		
		case 2:
			printf("xyz");
			break;

		case 3: 
			printf("pqr");
			break;

		default:
			printf("stu");
	} 
	
	return 0;
}

Output

xyz

Here, a is an expression which returning an integer value since it's value is 2 case 2 will be executed. Note that as we have used break statement here only case 2 will be executed and after that control will come out of the closing brace of the switch.

Note: Also keep in mind that switch, case, default are all keywords.

Features of switch

  • To execute multiple statements, in any case, there is no requirement of braces as in if else.
  • The default may or may not use the break statement.
  • We can use case number in ascending or descending order or in any random order.
  • In both switch and case, we use int or char constant.
  • We can use default anywhere in the program because every time default is executed only when there is no match of any case.
  • We can execute a common set of statement for multiple cases.
  • If there is any statement in switch then it must be in any case.

Executing a common set of statements for multiple cases

#include <stdio.h>

int main()
{
	int a = 2 ;
	
	switch(a)
	{
		case 1:
		case 2:
			printf("xyz");
			break;

		case 3: 
			printf("pqr");
			break;

		default:
			printf("stu");
	} 
	return 0;
}

Output

    xyz

Here, if even case 1 is encountered xyz is printed.

Disadvantages of switch statements

  1. float constant cannot be used in the switch as well as in the case.
  2. You can not use the variable expression in case.
  3. You cannot use the same constant in two different cases.
  4. We cannot use the relational expression in case.

Difference between switch case and if-else

Sr No The switch case The if - else
1 In case of a switch, we create jump table on compile time only selected case is executed on runtime. In this case, we do not create a jump table and all cases are executed at runtime.
2 If a program is large we use the switch statement. If a program is large then program structure will be more complex.
3 If a program is large, then switch case gives the more structured program. If a program is small we use if else.

Using switch case to design calculator

#include<stdio.h>

int main()
{
	int num1,num2,opt;

	printf("Enter the first Integer: ");
	scanf("%d",&num1);
	printf("Enter the second Integer: ");
	scanf("%d",&num2);

	printf("1:addition \n2: subtraction \n3: multiplication \n4: division\n");
	printf("Enter an correct option...: ");
	scanf("%d",&opt);

	switch(opt)
	{
		case 1:
			printf("\nAddition of  %d and %d is: %d",num1,num2,num1+num2);
			break;

		case 2:
			printf("\nSubstraction of %d  and %d is:  %d",num1,num2,num1-num2);
			break;

		case 3:
			printf("\nMultiplication of %d  and %d is:  %d",num1,num2,num1*num2);
			break;  

		case 4: 
			if(num2==0)
			{
				printf("OOps Devide by zero\n");
			}
			else
			{
				printf("\n Division of %d  and %d is:  %d",num1,num2,num1/num2);
			}  
			break;

		default:
		printf("\n Enter correct option\n");
	}

	return 0;
}

Output

    Enter the first Integer: 10
    Enter the second Integer: 6
    1:addition 
    2: subtraction 
    3: multiplication 
    4: division
    Enter an correct option...: 3
    Multiplication of 10  and 6 is:  60



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.