Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases).
In C, the switch case statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder.
The switch statement consists of conditional-based cases and a default case.
switch(expression) case value1: statement_1; break; case value2: statement_2; break; . . . case value_n: statement_n; break; default: default_statement; >
Before using the switch case in our program, we need to know about some rules of the switch statement.
Following are some of the rules that we need to follow while using the switch statement:
Case 1 is Matched.
The working of the switch statement in C is as follows:
We can also understand the working of the switch statement in C using the flowchart.
Flowchart of switch statement in C
This keyword is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
The break statement is optional . If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
Case 2 is executed. Case 3 is executed.Case 4 is executed.
The default keyword is used to specify the set of statements to execute if there is no case match .
It is optional to use the default keyword in a switch case. Even if the switch case statement does not have a default statement, it would run without any problem.
If the expression provided in the switch statement does not result in a constant value, it would not be valid. Some valid expressions for switch case will be,
// Constant expressions allowed switch(1+2+23) switch(1*2+3%4) // Variable expression are allowed provided // they are assigned with fixed values switch(a*b+c*d) switch(a+b+c)
The switch statement can only evaluate the integer or character value. So the switch expression should return the values of type int or char only.
In the C switch statement, duplicate case values are not allowed.
Nesting of switch statements is allowed , which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex and less readable.
Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn’t change the core logic.
The day with number 2 is Tuesday
Output
Enter the operator (+, -, *, /) Enter x to exit + Enter the two numbers: 100 + 200 100 + 200 = 300
In this article, we discussed the switch statement in C programming and how to use it. It is a conditional statement like the if-else-if ladder having its own merits and demerits. It is mostly preferred when the number of conditions to evaluate is large.
The switch case statement is a flow control statement in which we can define a switch variable and then execute different code based on the value of the switch variable. It is an alternative of if else if ladder.
The case keyword is used to define the different cases and their associated code in the switch statement.
The break keyword is used to exit the switch block after executing the matching case.
Must Read: