switch statement in C-Programming
switch statement in C-Programming
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).
• Switch case statements follow a selection-control mechanism and allow a value to change
control of execution.
• They are a substitute for long if statements that compare a variable to several integral values.
• The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
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.
Following are some of the rules that we need to follow while using the switch statement:
1. In a switch statement, the "case value" must be of "char" and "int" type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.
Example
return 0,
Output
Case I is Matched.
How switch Statement Work?
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.
// C Programto demonstrate
the behaviourof switchcase
// withoutbreak
#include <stdio.h>
int main()
int var
// switch case withoutbreak
switch (var)
case 1
printf( "Case 1 is executed.\n");
case 2
printf("Case 2 is executed.\n");
case 3
printf("Case 3 is executed.
case 4
printf( '"Case4 is executed.
return 0,
Output
Case 2 executed .
Case 3 executed. Case 4 is executed.
Default in switch case
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,
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.
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.
Examples of switch Statement in C
Example 1: C Program to print the day of the week using a switch case.
Output
return 0,
Output
Conclusion
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
variableand then executedifferentcode based on the value of the switch variable.It is an
alternativeof if else if ladder.
The case keywordis used to define the differentcases and their associated code in the switch
statement.
The break keywordis used to exit the switch blockafter executingthe matching case.
switch
• It executes the differentcases on the basis of the value of the switch variable.
• It can only evaluate the int or char type expressions.
• Faster and easier to read for the large number of conditions.
if else if
You all are familiar with switch case in C, but did you know you can use a range of numbers
instead of a single number or character in the case statement? Range in switch case can be useful
when we want to run the same set of statements for a range of numbers so that we do not have to
write cases separately for each value.
• That is the case range extension of the GNU C compiler and not standard C.
• You can specify a range of consecutive values in a single case label.
Syntax
case 'A'
You need to Write spaces around the ellipses ( ). For example, write this:
// Correct case 1 5:
// Wrong - case 1...5.
c
C program to illustrate
// using range in switch case
*include < stdio.h>
int main ()
for (int 14
switch arr[i])
// range 1 to 6
case 1 6:
printf( "Ed in range arr 1
break;
// range 19 to 20
case 19
print f ( in range 19 to 20\n' arr 1
break;
default:
print f ( not in range\n• arr[i]);
break;
return O ;
Output
1 in range 1 to 6
5 in range 1 to 6
15 not in range
20 in range 19 to 20
Complexity Analysis
Error conditions