In the C programming language, the switch-case statement provides a way to execute different blocks of code based on the value of a variable or an expression. It allows for more concise and organised code compared to using multiple if-else statements. Here's an example to illustrate the usage of the switch-case statement:
c#include <stdio.h>
int main() {
int choice;
printf("\n\n\t\tWelcome to Menu:");
printf("\n\t1. Option 1");
printf("\n\t2. Option 2");
printf("\n\t3. Option 3");
printf("\n\tEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nYou selected Option 1.");
// Additional code related to Option 1 can be placed here
break;
case 2:
printf("\nYou selected Option 2.");
// Additional code related to Option 2 can be placed here
break;
case 3:
printf("\nYou selected Option 3.");
// Additional code related to Option 3 can be placed here
break;
default:
printf("\nInvalid choice.");
// Additional code for handling invalid choices can be placed here
break;
}
return 0;
}
In this example, the user is presented with a menu and asked to enter their choice. The value entered by the user is stored in the choice
variable. The switch statement is then used to evaluate the value of choice
.
If choice
matches one of the case labels (1, 2, or 3), the corresponding block of code is executed. For example, if the user enters 2, the code under case 2:
will execute, printing "You selected Option 2." The break
statement is used to exit the switch statement once a match is found and prevent the execution of subsequent case blocks.
If the value of choice
doesn't match any of the case labels, the code under the default:
label will execute. In this example, it prints "Invalid choice." The default
case is optional and is executed only if none of the other case labels match the value of the variable.
It's important to include break
statements after each case block to prevent fall-through, where the code execution continues to the next case block without any matching condition.
Here's some additional information about the switch-case statement in C:
Fall-Through Behaviour:
By default, each case block in a switch statement ends with abreak
statement. This is important to prevent fall-through behaviour, where execution continues to the next case block even if it doesn't match the value being evaluated. However, sometimes fall-through behaviour is desired. In such cases, thebreak
statement can be omitted, and execution will continue to the next case block until abreak
statement or the end of the switch statement is encountered.Multiple Cases:
In some scenarios, you may want multiple case labels to execute the same block of code. To achieve this, you can list multiple case labels together without any code between them. For example:
cswitch (choice) {
case 1:
case 2:
printf("\nYou selected Option 1 or Option 2.");
// Additional code related to Option 1 and Option 2 can be placed here
break;
case 3:
printf("\nYou selected Option 3.");
// Additional code related to Option 3 can be placed here
break;
default:
printf("\nInvalid choice.");
// Additional code for handling invalid choices can be placed here
break;
}
In this example, if the user enters either 1 or 2, the code under case 1:
and case 2:
will execute, printing "You selected Option 1 or Option 2."
Constant Expressions:
The case labels in a switch statement must be constant expressions, which means they should be known at compile-time. This restriction allows the compiler to optimize the code and perform efficient jumps to the correct case block.Switch-case and Char Data Type:
The switch-case statement can also be used with thechar
data type. Sincechar
values are essentially integer values, they can be used as case labels. For example:
cchar grade = 'B';
switch (grade) {
case 'A':
printf("\nExcellent!");
break;
case 'B':
printf("\nGood job!");
break;
case 'C':
printf("\nFair enough.");
break;
default:
printf("\nYou need to improve.");
break;
}
In this example, if the value of the grade
variable is 'B', it will match the case label 'B'
, and the corresponding block of code will execute, printing "Good job!"
The switch-case statement is a powerful control structure in C that provides a concise and organized way to handle multiple possible values of a variable. It offers an alternative to long chains of if-else statements and can improve code readability and maintainability.