In this article Switch statement in C we give the information about The Switch Statement is a Decision Making Control Statement which we use when we have many options and we have to execute only one of those options.
Switch Statement in C:
Introduction:-
The Switch Statement is a Decision Making Control Statement which we use when we have many options and we have to execute only one of those options.
The Switch statements are an alternative to a larger if else statement. we can do all the work done by the if else statement with the help of Switch Case Statement.
We cannot evaluate the expression in the switch statement’s parenthesis () as True or False. We match that expression with the constant values written after the Case keyword in the body of the Switch Statement and if the expression matches any constant value, then the code after that case is executed.
switch (variable or an integer expression)
{
case constant_value1 :
// Code to be executed if expression == constant_value1
break;
case constant_value2 :
// Code to be executed if expression == constant_value2
break;
case constant_value3:
// Code to be executed if expression == constant_value3
break;
………..
default : // the default case is optional
// Code to be executed if none of the cases match
}
Program of switch case statement:
void main()
{
int number=0;
printf(“Enter a number\n:”);
scanf(“%d”,&number);
switch(number)
{
case 101: printf(“number is equals to 101”); break;
case 102: printf(“number is equal to 102”); break;
case 103: printf(“number is equal to 103”); break;
Default: printf(“number is not equal to 101, 102 or 103”);
}
}
Output -:
Enter a number 101
number is equals to 101
Important Points about Switch Statement in C:
Whatever expression occurs in the parenthesis of the Switch Statement will provide a constant value at the end. If the result of the expression does not provide a constant value, then that expression will not be valid.
Valid Expression
switch(11+12+123)
switch(11*12+13)
Invalid expression
switch(xy+yz)
switch(x+y+z)
- In the parenthesis () of the switch statement, there will be either an Integer Constant or a character constant.
Example -: switch(16+12) or switch(b)
- The constant value after the Case keyword inside the switch block cannot be used in more than one case, or in other words, duplicate case values are not allowed.
Invalid case value
case ‘P’ : code;
break;
case ‘P’ : code;
break;
Valid case value
case ‘P’ : code;
break;
case ‘Q’ : code;
break;
- It is not necessary to write the switch case constant in the same order like 11, 12, 13, 14, you can write it in any order (like 12, 14, 13, 11).
Example:
switch(expression)
{
case 12 : code;
break;
case 11 : code;
break;
case 13 : code;
break;
default : code:
}
- We can do nesting inside the switch statement, but nesting will complicate the code a bit, so if I believe, then it is better if you do not do nesting inside the switch case.
- The default case we write inside the switch block is optional, even if you do not write the default case, it will work. But in my opinion, you should write a default case so that the program remains correct.
- After the Case keyword which is Constant, after that we have to put colon “:”, this colon defines that case.
- In every case, it is optional to write the break keyword at the end of the code. If you want, do not write the break keyword after the code.
But, if you do not use the break keyword, then all the other cases after that case will be executed.
So you should use break when you do not have to execute the code of any other case after the code of any case is executed.
Let us understand this with a program:
int main()
{
int i=12;
switch (i)
{
case 11: printf(“Case11 “);
case 12: printf(“Case12 “);
case 13: printf(“Case13 “);
case 14: printf(“Case14 “);
default: printf(“Default “);
}
return 0;
}
Output -:
Case12 Case13 Case14 Default
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining