‘switch’ statement in C
Let us consider a situation in which we have more than one options out of which we need to select/recognise only one option that is to be executed. Such kind of problems can be answered using nested if statement.
But as the number of options increases, the complexity of the program also gets increased and program become lengthy . This type of problem can be solved very easily by using switch statement. Using switch statement, one can select only one option from more number of options very easily.
In switch statement, we consider a value that is to be compared with a value associated with every option. Whenever the given value is same/similar with the value associated with an option, the execution of program starts from that option. In switch statement every option is defined as a ‘case’.
Syntax of switch statements along with the execution of flow chart diagram…
switch (expression or value)
case value 1: set of statements;
case value 2 : set of statements;
case value3 : set of statements;
case value4: set of statements;
Default : set of statements;
The switch statement contains one or more number of cases and each case has a value associated with it. At first switch statement compares the first case value with the switchValue, if it gets matched the execution starts from the first case. If it doesn’t match the switch statement compares the second case value with the switchValue and if it is matched the execution starts from the second case. This process continues until it finds a match. If no case value matches with the switchValue specified in the switch statement, then a special case called default is executed.
When a case value matches with the switchValue, the execution starts from that particular case. This execution flow continues with next case statements also. To avoid this, we use “break” statement at the end of each case. That means the break statement is used to terminate the switch statement. However it is optional.
Example Program | Display pressed digit in words.
int n ;
printf(“Enter any digit: “) ;
scanf(“%d”, &n) ;
switch( n )
case 0: printf(“ZERO”) ;
case 1: printf(“ONE”) ;
case 2: printf(“TWO”) ;
case 3: printf(“THREE”) ;
case 4: printf(“FOUR”) ;
case 5: printf(“FIVE”) ;
case 6: printf(“SIX”) ;
case 7: printf(“SEVEN”) ;
case 8: printf(“EIGHT”) ;
case 9: printf(“NINE”) ;
default: printf(“Not a Digit”) ;
Enter any digit :5
Enter any digit :90
Not a digit
MOST IMPORTANT POINTS TO BE REMEMBERED
When we use switch statement, we must follow the following…
- Both switch and case are keywords so they must be used only in lower case letters.
- The data type of case value and the value specified in switch statement must be same.
- switch and case values must be either integer or character but not float or string.
- A switch statement can contain any number of cases.
- The keyword case and its value must be superated with a white space.
- The case values need not be defined in sequence, they can be in any order.
- The default case is optional and it can be defined anywhere inside the switch statement.
- The switch value might be a direct value, a variable or an expression.