The switch statement is used to choose one option from several options. It is a decision making statement. When the value of expression inside the switch is matched to case value then the statements following that case will execute until the break.
Syntax for switch statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
switch(expression) { case value1: statement 1; break; //optional case value2: statement 2; break; //optional . . . case value n: statement n; break; //optional default: statement; break; //if all cases are not matched } |
Flowchart of switch with break keyword:
The operation of switch is shown below in the form of a flowchart for a better understanding.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
int main() { int a; printf("Please choose a number from 1 to 5\n"); scanf("%d",&a); switch(a) { case 1: printf("You have choosen 1"); break; case 2: printf("You have chosen 2"); break; case 3: printf("You have chosen 3"); break; case 4: printf("You have chosen 4"); break; case 5: printf("You have chosen 5"); break; default : printf("Invalid Choice."); break; } } |
Output:
1 2 3 |
Please choose a number from 1 to 5 5 You have choosen 5 |
Rules for switch statement
- The expression inside switch must be of integer or character type.
- switch expression cannot be float or double value.
- Also, switch expression cannot be NULL or string. But case can be NULL.
- The case can be integer constant, a character constant.
- The value of case must be of same data type as a value of an expression in the switch.
- Cases can never have variable expressions (for example case a +3 : )
- break takes the control outside the switch and it is optional
- Not every case needs break keyword.
- If there is no break then all the cases will be executed after matching case. And this is known as fall-through state of the switch statement.
- You cannot use continue keyword in the switch.
- Duplication of case is not allowed.
- You can have any number of case values.
- switch statement has the default case when all other cases are not matched then default statement is executed.
- The default case does not require break keyword. The default case is optional.
Forms of switch
- Switch expression cannot be NULL.
1234switch(NULL) //error{case 0 : printf("hi");} - The switch case value can be NULL.
1234switch('a'){case NULL : printf("hi"); //hi} - Following examples are valid.
1234switch('a'){case 97 : printf("hi"); //hi}
1234switch(97){case 'a' : printf("hi"); //hi} -
123456789switch(97){case 97 : printf("hi\n");case 96 : printf("hello");}/* Output: hihello*/
-
12345678switch(97){case 97 : printf("hi");break;case 96 : printf("hello");}/* Output: hi */
- Duplication of case value is not allowed.
12345switch(97){case 97 : printf("hi");case 97 : printf("hello"); //error(ambiguity)}
12345switch(0){case '\0' : printf("hi");case NULL : printf("hello"); //error(ambiguity)} - You are also allowed to use char values in cases and switch.
12345switch('K'){case 'U'-10 : printf("hi"); //allow}/* output: hi */ - Here 10 > 2 is true. Since true means 1 it executes case whose value is 1.
1234567switch(10 > 2){case 0 : printf("hi\t");case 1 : printf("hello\t");case 10 : printf("good\t");case 2 : printf("bye");}
Output:
1hello good bye
Here also, switch expression is true. And 1 && 1 is equal to 1 i.e. true, hence it will execute.
1234567switch(10 > 2){case 0 : printf("hi\t");case 1 && 1 : printf("hello\t");case 10 : printf("good\t");case 2 : printf("bye");}
Output:
1hello good bye -
1234switch(1); //error(termination of switch){case 1 : printf("hi");}
-
123456switch(1);{printf("hi");}/* output: hi */
- At times we may want to execute a common set of statements for multiple cases.
12345678910111213141516171819202122232425#include<stdio.h>int main( ){char ch ;printf ( "Enter any of the alphabet a, b, or c\n " ) ;scanf ( "%c", &ch ) ;switch ( ch ){case 'a' :case 'A' :printf ( "apple" ) ;break ;case 'b' :case 'B' :printf ( "ball" ) ;break ;case 'c' :case 'C' :printf ( "cat" ) ;break ;default :printf ( "wish you knew what are alphabets" ) ;}} - Even if there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else).
- Even if a switch expression doesn’t belong to any case the compiler won’t report an error.
1234567switch(2){case 3 : printf("value is 3");case 10 : printf("value is 10");}/* no output */