Switch Statement in C Language – BCA Notes
The switch statement in C is another type of decision-making statement. It allows the program to choose from multiple options based on the value of a variable. In this post by BCA School, you’ll learn the syntax, working, and examples of the switch statement in C.
🔹 What is a Switch Statement?
A switch statement tests a variable against multiple values. When a match is found, the corresponding block of code is executed. It is more efficient than multiple if-else statements for handling multiple conditions.
🔹 Syntax of Switch Statement
switch(expression) {
case value1:
// statements
break;
case value2:
// statements
break;
...
default:
// statements
}
🔹 Example of Switch Statement
#include <stdio.h>
int main() {
int day = 3;
switch(day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}
return 0;
}
🔹 Output
Wednesday
🔹 Key Points About Switch Statement
- Used to replace multiple
if-elsestatements when testing a single variable. - Each
casemust end with abreakto prevent “fall-through”. defaultblock executes when none of the cases match.- Switch works only with integer, character, or enumerated values.
🎯 Conclusion
The switch statement simplifies decision-making for multiple conditions and makes code cleaner and more readable. BCA students should practice switch statements along with if-else to handle different scenarios efficiently.
