SWITCH-CASE in C

  • A control statement that allows us to make a decision from the number of choices is called a switch case statement
  • The expression used in a switch statement can be an integer or a character constant
  • The constant in each case should be different, So for integer constant like 1,2,3,4… and for char constants like ‘A’, ‘B’, ‘C’ ….and so on or ‘a’, ‘b’, ‘c’….. and so on.
  • In C and C++ the single quotation(‘ ‘) is used to identify the character
  • The constant value is given as input is matched in the cases, one by one. If the constant value is matched then that case will be executed with the default statement
  • If no match is found with any of the case statements, only the statements following the default are executed

flow chart of switch case:

Switch case
Switch case in C

Examples of switch case:

Write a simple program using switch case:

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Enter the choice\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("This is Case One");
break;
case 2:
printf("This is Case Two");
break;
case 3:
printf("This is Case Three");
break;
default:
printf("This default case");
}
getch();
}

OUTPUT:

Enter the choice
1
This is Case One

Write a simple program using switch case without a break statement

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Enter the choice\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("This is Case One\n");
case 2:
printf("This is Case Two\n");
case 3:
printf("This is Case Three\n");
default:
printf("This default case\n");
}
getch();
}

OUTPUT:

Enter the choice
1
This is Case One
This is Case Two
This is Case Three
This default case

We are making use of the fact that once a case is satisfied, the control simply falls through case until it doesn’t encounter a break statement.Break statement is not compulsory in the switch case but if the break statement is not written everything will be executed after the case statements

Default is not necessary but in case if default is not written and some case occurs which is not considered then there will be no operation performed and the user will not be able to realize the problem in the program.

Write a program to display the user entered single-digit number in words

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a single digit number:");
scanf("%d",&n);
switch(n)
{
case 0:printf("Zero");
break;
case 1:printf("One");
break;
case 2:printf("Two");
break;
case 3:printf("Three");
break;
case 4:printf("Four");
break;
case 5:printf("Five");
break;
case 6:printf("Six");
break;
case 7:printf("Seven");
break;
case 8:printf("Eight");
break;
case 9:printf("Nine");
break;
}
getch();
}

OUTPUT:

Enter a single digit number:4
Four

Write a program to display any user-entered number in words

#include<stdio.h>
#include<conio.h>
void main()
{
int n,digit,rev=0;
clrscr();
printf("Enter a number");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
rev=rev*10+digit;
n=n/10;
}
while(rev!=0)
{
digit=rev%10;
rev=rev/10;
switch(digit)
{
case 0:printf("Zero");
break;
case 1:printf("One");
break;
case 2:printf("Two");
break;
case 3:printf("Three");
break;
case 4:printf("Four");
break;
case 5:printf("Five");
break;
case 6:printf("Six");
break;
case 7:printf("Seven");
break;
case 8:printf("Eight");
break;
case 9:printf("Nine");
break;
}
}
getch();
}

OUTPUT:

Enter a number:213
TwoOneThree

Write a menu-driven program to perform to add/subtract/multiply/divide/modulus based on the user choice

#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,result,choice;
clrscr();
printf("Enter two numbers:");
scanf("%d%d",&no1,&no2);
printf("1.Add\n2.Subtract\n3.Multiply\n4.Divide\n5.Modulus\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:result=no1+no2;
printf("Sum=%d",result);
break;
case 2:result=no1-no2;
printf("Difference=%d",result);
break;
case 3:result=no1*no2;
printf("Product=%d",result);
break;
case 4:result=no1/no2;
printf("Quotient=%d",result);
break;
case 5:result=no1%no2;
printf("Remainder=%d",result);
break;
default:printf("Invalid choice");

}
getch();
}

OUTPUT:

Enter two number:5
2
1.Add
2.Subtract
3.Multiply
4.Divide
5.Modulos
Enter your choice:1
Product=7

Difference between if-else, switch, ladder if-else, and nested if-else

if else statementswitch statementladder if else statementnested if else statement
In this we can test only one condition In this case we can test multiple conditions In this case we can test multiple conditions In this case we can test multiple conditions
In this we cannot have different conditionsIn this case we can have only one expression but various value of the same expressionIn this case we can have multiple conditions independent of each otherIn this case we can have multiple conditions independent of each other
This is used when there is only one condition and one value of the same to be tested This is used when there is only one condition but multiple values of the same to be tested This is typically used when there are multiple conditions to be testedThis is typically used when there are multiple conditions to be tested
It can have values based on constraintsIt can have values based on user choiceIt can have values based on constraintsIt can have values based on constraints
In this first the condition is tested and then it comes to else if tthe condition is not trueIn this case first the case is checked and then it switches to that caseIn this first the condition is tested and then it comes to else if the condition is not true and then the other conditions are testedIn this first the condition is tested and then it comes to else if the condition is not true and then the other conditions are tested
If else statement is used to evaluate a condition to be true or falseA switch case statement is used to test for multiple values of the same variable or expression like 1,2,3 etcLadder if else is used to test multiple conditions to be true of false and accordingly perform various tasks according to the correct conditionIn a nested if else, we can have multiple conditions evaluated based on the previous condition to be true to decide whether to perform a task or not

Translate »