C Switch Statement
- The switch statement in C language is used to execute the code from multiple conditions.
- It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
Rules for switch statement in C language
- The switch expression must be of integer or character type.
- The case value must be integer or character constant.
- The case value can be used only inside the switch statement.
- The break statement in switch case is not must.
- It is optional.
- If there is no break statement found in switch case, all the cases will be executed after matching the case value.
- It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
Let's see a simple example of c language switch statement.
Output
enter a number:4number is not equal to 10, 50 or 100
enter a number:50number is equal to 50
C Switch statement is fall-through
- In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.
- Let's try to understand the fall through state of switch statement by the example given below.