C – switch case statement in C Programming with example




C – switch case statement in C Programming with example


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

C – Switch Case Statement

Syntax:
switch (variable or an integer expression)
{
case constant:
//C Statements
;
case constant:
//C Statements
;
default:
//C Statements
;
}

Flow Diagram of Switch Case

Example of Switch Case in C

#include <stdio.h>
int main()
{
int num=2;
switch(num+2)
{
case 1:
printf
("Case1: Value is: %d", num);
case 2:
printf
("Case1: Value is: %d", num);
case 3:
printf
("Case1: Value is: %d", num);
default:
printf
("Default: Value is: %d", num);
}
return 0;
}
Output:
Default: value is: 2
Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4 the default case is executed.

Twist in a story – Introducing Break statement

Before we discuss more about break statement, guess the output of this C program.
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf
("Case1 ");
case 2:
printf
("Case2 ");
case 3:
printf
("Case3 ");
case 4:
printf
("Case4 ");
default:
printf
("Default ");
}
return 0;
}
Output:
Case2 Case3 Case4 Default
I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2, However there are no such statements in the above program which could break the flow after the execution of case 2. That’s the reason after case 2, all the subsequent cases and default statements got executed.
How to avoid this situation?
We can use break statement to break the flow of control after every case block.

Break statement in Switch Case

Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.
Example of Switch Case with break
I’m taking the same above that we have seen above but this time we are using break.
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf
("Case1 ");
break;
case 2:
printf
("Case2 ");
break;
case 3:
printf
("Case3 ");
break;
case 4:
printf
("Case4 ");
break;
default:
printf
("Default ");
}
return 0;
}
Output:
Case 2
Why didn’t I use break statement after default?
The control would itself come out of the switch after default so I didn’t use it, however if you want to use the break after default then you can use it, there is no harm in doing that.

Few Important points regarding Switch Case

1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any integer value after case keyword. Also, case doesn’t need to be in an ascending order always, you can specify them in any order as per the need of the program.
2) You can also use characters in switch case. for example –
#include <stdio.h>
int main()
{
char ch='b';
switch (ch)
{
case 'd':
printf
("CaseD ");
break;
case 'b':
printf
("CaseB");
break;
case 'c':
printf
("CaseC");
break;
case 'z':
printf
("CaseZ ");
break;
default:
printf
("Default ");
}
return 0;
}
Output:
CaseB
3) The expression provided in the switch should result in a constant value otherwise it would not be valid.
For example:
Valid expressions for switch –
switch(1+2+23)
switch(1*2+3%4)
Invalid switch expressions –
switch(ab+cd)
switch(a+b+c)
4) Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.

Post a Comment

Previous Post Next Post