Switch Case in C
switch case provides a way to solve some specific problem very easily
Swithch case statement is also another way of making decison based on various conditions. Switch case can be more effective and useful in various cases where we can use multiple if statements of else if statement. The switch statement tests the value of a given varible ( or expression ) against a list of case values and when a match is found, a block of statemens associated with that case is executed. The syntax of switch case is shown below.
switch Case Syntax
Rules of switch statement in C
- The expression can be either an integer expression or character
- If expression is an integral expression then in must be evaluable to an integral constant.
- Value_1, Value_2,… can be constant or constant expression.
- value_1, value_2,… must be unique value and must end with : mark.
- if value_1, value_2,… are constant expression then the constant expression must be evaluable to an integral constant.
- The block_1, block_2,… may contain zero or more than zero statements.
- At the end of each block_1, block_2 … use break; to terminate switch case after valid match case found.
- default is optional, if no match found then default statement executes.
switch Case Working
switch case statement fallows the below listed sequance.
- When the switch is executed, the value of the expression will be compared against the values value_1, value_2,…
- If any value matches with the value of expression then the block of that case will execute
- The break; indicates the end of that block and termination of switch case.
- If no case value matches then default value executes.
- After the switch statement, the statement-M will execute.
C switch statement block diagram
Example 1
Write a program in C using switch case. The program should take an input number between 1 to 10 and in the output, the program should print that number in words.
Output
Example 2
Write a program in C which takes an even number between 1 to 10, In output program prints squire of the number. If all case is not satisfied then print Invalid Number as output message. Use the switch case to solve the problem.
Output
switch Case without break;
What is the output of the below program?
Output
Explanation
If you are not using break; in blocks then after match case all block will executed including default block, if there.
Note: break; statement is not mandatory, but proper use of "break;" statement increase the program efficiency.
Valid and Invalid Expression in switch statement
See below declaration and valid and invalid expression examples in table
Valid and Invalid switch expressions |
Valid Expression |
Invalid Expression |
switch(a) |
switch(f) |
switch(a>b) |
switch(a+3.5) |
switch(x+y-3) |
|
switch(func(b,c)) |
|
- The switch expression must be of integer or character type.
- Float value is not allowed as switch expression.
- switch statement cannot evaluate relational operators i.e they are not allowed in switch statement.
Valid and Invalid case in switch statement
See below declaration and valid and invalid case examples in table
Valid and Invalid switch case |
Valid case |
Invalid case |
case 5 |
case 6.5 |
case 'x' |
case a |
case 3+2 |
case x+5 |
case 'x'>'y' |
case 5,6 |
Difference between switch and if statement
- You can work with float condition in if statement where as you can’t work with float condition in switch statement.
- You can work with relational operator in if statement where as you can’t work with relational operator in switch statement.