Switch Case in C Programming
Hello Everyone, In this article, we will cover the concept of Switch Case in C programming. In Switch Statement, we will cover basic syntax, functionality, and practical examples to help you understand how to effectively implement it in your program. Whether you’re new to C or have some experience, this guide will help you to understand C Switch Statement effectively. Let’s get started.
Switch Case is a control structure in the C programming language that allows the program to execute different code segments based on the value of a variable or an expression. It is often used as an alternative to a series of if-else statements.
The switch statement takes an expression as input and evaluates it against a series of constant values called “case labels.” If the value of the expression matches one of the case labels, the code segment following that label is executed. If none of the case labels match the value of the expression, the code segment following the default label is executed, if present.
Basic Syntax of Switch Statement
switch(expression) {
case constant1:
// code to be executed if expression matches constant1
break;
case constant2:
// code to be executed if expression matches constant2
break;
...
case constantN:
// code to be executed if expression matches constantN
break;
default:
// code to be executed if expression does not match any constant
break;
}
The break keyword is used to terminate the current case and exit the switch statement. If break is not used, execution will continue to the next case label, which can result in unexpected behavior.
Switch statements can be useful for simplifying code and making it easier to read and maintain. However, they should be used with caution, as they can become difficult to manage if there are too many case labels or if the logic becomes too complex.
write the c program to explain the switch case
C Program that demonstrates the use of switch case
#include <stdio.h>
int main() {
int num;
printf("Enter a number between 1 and 3: ");
scanf("%d", &num);
switch(num) {
case 1:
printf("You entered 1.\\n");
break;
case 2:
printf("You entered 2.\\n");
break;
case 3:
printf("You entered 3.\\n");
break;
default:
printf("Invalid number entered.\\n");
break;
}
return 0;
}
In this program, the user is prompted to enter a number between 1 and 3. The value is stored in the variable “num”, which is then used in the switch statement.
If the value of “num” matches one of the case labels (1, 2, or 3), the corresponding code segment is executed and the program exits the switch statement using the “break” keyword. If the value of “num” does not match any of the case labels, the code segment following the default label is executed.
For example, if the user enters the number 2, the output of the program would be:
You entered 2.
If the user enters a number outside the range of 1 to 3, the output would be:
Invalid number entered.
I hope this helps to clarify the use of switch case in C programming!
Complex Program to Explain Switch Case
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2lf - %.2lf = %.2lf\\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\\n", num1, num2, result);
break;
case '/':
if (num2 == 0) {
printf("Error: division by zero.\\n");
}
else {
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\\n", num1, num2, result);
}
break;
default:
printf("Invalid operator entered.\\n");
break;
}
return 0;
}
In this program, the user is prompted to enter an arithmetic operator (+, -, *, or /) and two numbers. The program then uses a switch statement to perform the appropriate arithmetic operation and display the result.
The switch statement evaluates the value of the “operator” variable and executes the code segment corresponding to the appropriate case label. If the operator is invalid, the code segment following the default label is executed.
Note that in the case of division, the program checks to make sure that the second number is not zero before performing the operation to avoid a divide-by-zero error. I hope this example helps to illustrate the flexibility and power of switch case in C programming!
Output
Enter an operator (+, -, *, /): *
Enter two numbers: 5.5 2.5
5.50 * 2.50 = 13.75
In this example, the user enters the operator “*” and the two numbers 5.5 and 2.5. The switch statement matches the operator to the case label for multiplication, calculates the product of the two numbers (which is 13.75), and prints the result in the format specified in the printf statement.