Understanding the switch
Statement in C: A Beginner’s Guide
The switch
statement in C is a powerful control structure that simplifies complex decision-making. It provides an alternative to using multiple if-else
conditions, especially when checking a variable against several fixed values.
In this blog, we’ll explore the syntax, use cases, and practical examples of switch
statements, along with detailed explanations.
1. What is a switch
Statement?
The switch
statement allows a variable to be tested for equality against multiple values. Each value is called a case, and the variable is compared to each case.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
...
default:
// Code to execute if no case matches
}
Key Points:
- The
expression
must evaluate to an integer or character. - The
case
keyword is followed by a constant value. break
exits theswitch
block. Without it, execution continues to the next case (fall-through behavior).- The
default
case is optional and runs if no case matches.
2. Basic Example
Let’s start with a simple example: selecting a day of the week based on user input.
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7) for the day of the week: ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please enter a number between 1 and 7.\n");
}
return 0;
}
Output (Examples):
- Input:
3
Wednesday
- Input:
8
Invalid input! Please enter a number between 1 and 7.
3. Practical Applications of switch
1. Calculator Program
This program performs basic arithmetic operations (+, -, *, /) based on the user’s choice.
#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("Result: %.2lf\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator!\n");
}
return 0;
}
Output (Examples):
- Input:
+
and5 3
Result: 8.00
- Input:
/
and4 0
Error: Division by zero is not allowed.
2. Grading System
This program assigns grades based on marks using a switch
statement.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks (0-100): ");
scanf("%d", &marks);
switch (marks / 10) {
case 10:
case 9:
printf("Grade A\n");
break;
case 8:
case 7:
printf("Grade B\n");
break;
case 6:
case 5:
printf("Grade C\n");
break;
default:
if (marks >= 0 && marks < 50) {
printf("Fail\n");
} else {
printf("Invalid marks entered.\n");
}
}
return 0;
}
Output (Examples):
- Input:
85
Grade B
- Input:
45
Fail
3. Finding the Type of Triangle
This program determines the type of triangle based on user input for the sides.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter the three sides of the triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) { // Check if it's a valid triangle
switch ((a == b) + (b == c) + (a == c)) {
case 3:
printf("The triangle is Equilateral.\n");
break;
case 1:
printf("The triangle is Isosceles.\n");
break;
default:
printf("The triangle is Scalene.\n");
}
} else {
printf("The sides do not form a valid triangle.\n");
}
return 0;
}
Output (Examples):
- Input:
5 5 5
The triangle is Equilateral.
- Input:
3 4 5
The triangle is Scalene.
4. Advanced Example: Converting Numbers to Words
This program converts single-digit numbers into words.
#include <stdio.h>
int main() {
int number;
printf("Enter a single-digit number (0-9): ");
scanf("%d", &number);
switch (number) {
case 0:
printf("Zero\n");
break;
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Invalid input! Enter a single-digit number.\n");
}
return 0;
}
Output (Examples):
- Input:
5
Five
- Input:
12
Invalid input! Enter a single-digit number.
5. Key Points to Remember
- Use
break
statements to prevent fall-through. - The
default
case is optional but improves the program’s robustness. switch
works well when checking a variable against discrete, fixed values (e.g., days, grades, numbers).- Avoid using complex expressions or ranges directly in
case
. Use division, modular arithmetic, or preprocessing logic instead.
Summary
The switch
statement in C is a clean and efficient way to handle multi-condition scenarios where a variable is compared to fixed values. It is especially useful in programs like calculators, menu-driven applications, and simple decision-making tasks.
In the next blog, we’ll explore loops in C, which allow repetitive execution of code. Stay tuned!