Switch Statement In C - Let's Learn with Simple examples
The switch statement is used to solve multiple option type problems for menu-like programs, where one value is associated with each option. Switch case statement helps us minimize the use of if/else.
The value of Variable/Expression in switch case evaluates to return an integral value, which is then compared to the values in different cases, where it matches that block of code is executed, if there is no match, then default block is executed.Quick Reading: Amazing fact of a Switch statement in C
- Fact 1- C Switch Statement: The statements/code written above cases are never executed.
- Fact 2 - C Switch Statement: The default block can be put anywhere you want.
- Fact 3 - C Switch Statement: switch statement accepts only integral type expression. (int, char).
- Fact 4 - C Switch Statement: Two case labels cannot have the same value.
- Fact 5 - C Switch Statement: All the statements following a matching case execute until a break statement is reached.
- Fact 6 - C Switch Statement: The integral expressions used in Case labels must be constant expressions.
- Fact 7 - C Switch Statement: There can be only one default label.
What is the syntax of the switch statement?
Syntax of Switch statement:
switch(Variable)
{
case value-1:
//code block-1;
break;
case value-2:
//code block-2;
break;
case value-3:
//code block-3;
break;
default:
//default-block;
break;
}
Learn in-depth about Switch Statment in C:
Interesting facts about switch statement in C with Example
Fact 1- C Switch Statement: The statements/code written above cases are never executed.
The control passes to the matching case after the switch statement, and the statements written before the case are not executed.Demonstration: What Happened when we write statements/code above "cases" in Switch statement
#include <stdio.h>
int main()
{
switch(p)
{
case 2:
printf("Value of p is %d",p);
break;
p = p +2;
case 4:
printf("Value of p is %d",p);
break;
default:
printf("No matched found and value of p is %d",p);
break;
}
return 0;
}
Output:
Value of p is 4
int p = 5;
switch (p)
{
case 2:
printf("Value of p is %d",p);
break;
default:
printf("No match found and value of p is %d",p);
break;
break;
}
return 0;
}
Output:
Value of p is 4
Fact 2 - C Switch Statement: The default block can be put anywhere you want.
It makes no difference where the default block is placed; it is still run if no match is found in the switch statement.
Demonstration: What Happened when You put The default block anywhere you want in Switch statement.#include <stdio.h>
int main()
{
int p = 5;
switch (p)
{
case 2:
printf("Value of p is %d",p);
break;
default:
printf("No match found and value of p is %d",p);
break;
case 4:
printf("Value of p is %d",p);
break;
}
return 0;
}
Output:
No match found and value of p is 5
⏩Watch fact of Switch statement in C - Step by step in Video Tutorial:⏩
Fact 3 - C Switch Statement: switch statement accepts only integral type expression. (int, char)
- Any other type of expression is not allowed.- Switch Statement Valid Expressions :
- Constant expressions: 5 + 6,'x' + 2, 'x', 8 *15 % 2, 20 / 4 + 2 etc, are the expressions evaluate to Integer constants, so they are valid.
- Variable expressions ( Taken int x,y; and char z): x, z * x - 2, x – y, z + 1, etc. int and char are considered integral and valid in the switch.
Demonstration: What Happened when You use non integral expression type in Switch statement.
#include <stdio.h>
int main()
{
float p = 5.5;
switch (p)
{
case 5.5:
printf("Value of p is %d",p);
break;
case 6:
printf("Value of p is %d",p);
break;
default:
printf("No matched found and value of p is %d",p);
break;
}
return 0;
}
Output:
[Error] switch quantity not an integer
Free Full C Programming Course With best Android app: Computer Courses
Computer Bits Daily
Fact 4 - C Switch Statement: Two case labels cannot have the same value.
Demonstration: What Happened when Two case labels have the same value in a Switch statement.
#include <stdio.h>
int main()
{
int p = 1;
switch (p)
{
case 1:
printf("Value of p is %d",p);
break;
case 1:
printf("Value of p is %d",p);
break;
default:
printf("No matched found and value of p is %d",p);
break;
}
return 0;
}
Compiler Error: duplicate case value
Fact 5 - C Switch Statement: All the statements following a matching case execute until a break statement is reached.
After the matching case statement is found, If the break statement is not present, then it will continue to execute all of the case statements, including the default statement. Let's see answer of Why break is used in switch statement?Demonstration: What Happened when there is no break in all cases in Switch statement.
#include <stdio.h>
int main()
{
int p = 2;
switch (p)
{
case 1:
printf("Value of p is %d \n",p);
case 2:
printf("Value of p is %d \n",p);
case 3:
printf("Value of p is %d \n",p);
case 4:
printf("Value of p is %d \n",p);
default:
printf("No match found and value of p is %d \n",p);
}
return 0;
}
int main()
{
int p = 2;
switch (p)
{
case 1:
printf("Value of p is %d \n",p);
case 2:
printf("Value of p is %d \n",p);
case 3:
printf("Value of p is %d \n",p);
case 4:
printf("Value of p is %d \n",p);
default:
printf("No match found and value of p is %d \n",p);
}
return 0;
}
Output:
2. Value of p is 2
3. Value of p is 2
4. Value of p is 2
No match found and value of p is 2
#include <stdio.h>
int main()
{
int p = 2;
switch (p)
{
case 1:
printf("1. Value of p is %d \n",p);
case 2:
printf("2. Value of p is %d \n",p);
case 3:
printf("3. Value of p is %d \n",p);
break;
int main()
{
int p = 2;
switch (p)
{
case 1:
printf("1. Value of p is %d \n",p);
case 2:
printf("2. Value of p is %d \n",p);
case 3:
printf("3. Value of p is %d \n",p);
break;
case 4:
printf("4. Value of p is %d \n",p);
default:
printf("No matched found and value of p is %d \n",p);
}
return 0;
}
Output:
printf("4. Value of p is %d \n",p);
default:
printf("No matched found and value of p is %d \n",p);
}
return 0;
}
Output:
2. Value of p is 2
3. Value of p is 2
Fact 6 - C Switch Statement: The integral expressions used in Case labels must be constant expressions.
Demonstration: What Happened if there we use variable expressions in case labels in Switch statement.#include <stdio.h>
int main()
{
int p = 2;
int a[2] ={1,2};
switch (p)
{
case a[0]:
printf("1. Value of p is %d \n",p);
break;
case 2:
printf("2. Value of p is %d \n",p);
break;
case 3:
printf("3. Value of p is %d \n",p);
break;
case 4:
printf("4. Value of p is %d \n",p);
break;
default:
printf("No matched found and value of p is %d \n",p);
break;
}
return 0;
}
Output:
[Error] an array reference cannot appear in a constant-expression
Check This out: Run C Program on android with termux
Check This out: Run C Program on android with termux
Fact 7 - C Switch Statement: There can be only one default label.
Demonstration: What Happened if there is more than one default label in Switch statement.
#include <stdio.h>int main()
{
int p = 2;
switch (p)
{
case 1:
printf("1. Value of p is %d \n",p);
case 2:
printf("2. Value of p is %d \n",p);
case 3:
printf("3. Value of p is %d \n",p);
break;
default:
printf("No matched found and value of p is %d \n",p);
case 4:
printf("4. Value of p is %d \n",p);
default:
printf("No matched found and value of p is %d \n",p);
}
return 0;
}
return 0;
}
Output:
[Error] multiple default labels in one switch
That's it for today. 😊 Happy Learning.
Please leave suggestions, queries, more about Switch Statement in the comment section.
Unlock Learning ✌ | Keep Sharing | Stay Safe.😷
0 Comments