What are the Switch Case Statement ???
Switch Case is Conditional Control Structure in C Language. Which is used to check a value of one variable and according that it executes a code blocks. In Other words, we say it is especially helpful when you want to compare the value of single variable or expression to the values of a many other contants.
Let’s understand Switch Case With it’s syntax,
Syntax:
Here is the syntax of Switch Case Statement which shows how the switch case works,
switch (variable) {
case value1:
// code block to execute if variable equals value1
break;
case value2:
// code block to execute if variable equals value2
break;
...
default:
// code block to execute if variable doesn't match any case
}
Here, “variable” is the variable whose value is being checked, and “valie1”, “value2”, etc., are the possible values it can take.
Now Let’s take and example to understand it briefly,
C Language E-Book
Example:
Here is the example of Switch Case Statement,
#include <stdio.h> int main() { char grade = 'B'; // Switch case statement example switch (grade) { case 'A': printf("Excellent!\n"); break; case 'B': printf("Good!\n"); break; case 'C': printf("Average!\n"); break; default: printf("Need Improvement!\n"); } return 0; }
Explanation about Example:
Here we use Switch Case Statement Which we used for check the performance of student on the basis of “grade” variable. Here we define different cases, according the true value the massage will be print which we set a Massage in our cases.