The switch statement in C programming languages is a robust, straightforward tool that aids decision-making. In real-world scenarios, making decisions such as choosing the right course, university, and career choices requires careful selection from the list of alternatives. However, you may need to make a better choice. The switch case statement in C simplifies the process of effective decision-making. It helps to select the right choice among the various options.
Switch Case Example in C Programming
For example, if you provide reviews for any product online, you either type 1, 2, 3, 4, or 5. If the input is 5, the display shows it as excellent. Every number is associated with one grade, such as good, very good, satisfactory, and bad for numbers 4, 3, 2, & 1, respectively.
The switch statement in C executes only that case that matches the given variable. In the above example, if the user provides 5 as the variable’s value inside the switch statement in C. The cursor searches for the case number that matches 5. After finding the appropriate case number, it executes the statement.
Importantly, the variable the programmer provides inside the switch statement’s expression. It should be either an integer or a character only. However, the switch statement will not execute a float value.
What are the Steps to Follow While Writing a Switch Statement:
Writing a program on a switch statement in C requires following a specific sequence of steps.
Step I: Declare a Variable (ex: int rating)
Step II: Specify the statement in printf() that needs to be shown. printf(“Rate a review (from 5 to 1): “);
Step III: Write scanf() to enable the user to enter the value. scanf(“%d”, &rating);
Step IV: Include the switch case statement in the C program to give values to the variable. switch(rating).
Step V: Next, provide flower brackets and include the cases.
{
case 5:
printf(“Excellentn”);
}
Step VI: Include Break
{
case 5:
printf(“Excellentn”);
break;
}
Step VII: We use the default if the user’s given value does not match any case.
Sample Switch Case Program in C
Break in Switch Statement
In switch, there are many cases, such as case 5, case 4, case 3, case 2, and case 1. After every case, the break statement must be included. Otherwise, the control propagates to the next case until it encounters the break, making all cases and even defaults to execute.
In the above example program, along with very good, the program generates good, satisfactory, bad, and rating invalid in the output. So, to generate the correct option in the output, a break statement must be included.
The Importance of Default in Switch:
Lastly, if the value doesn’t match with the case, it will execute the default.
For instance, if the user inputs an invalid input (outside 5 to 1), the program generates a rating of invalid while suggesting the user include numbers 5 to 1 alone.
Including a break after the default is unnecessary because the cursor exits the program when it reaches the end. However, the break statement is necessary if the default is in the middle or top of the cases.
To learn more about decision-making statements in C programming, Click Here.