The switch statement is a decision-making statement in C that allows a program to choose one block of code from multiple possible options. It is especially useful when a single expression needs to be compared with several constant values.
switch statement normally uses
case labels and a break statement. The optional
default block executes when no case matches.
The switch statement provides multiple choices based on the
value of an expression.
switch (expression)
{
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}
A switch statement is useful when one expression needs to be
compared against several discrete constant values.
int day = 2;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
}
This can be easier to read than a long sequence of equality comparisons.
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
default:
// statements
}
The expression is evaluated and compared with the case constants.
A case label specifies a possible constant value for the
switch expression.
int choice = 2;
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
}
Because choice is 2, the second case is selected.
The break statement terminates the switch statement and
continues execution after it.
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
}
Using break prevents execution from continuing into the next
case.
int number = 2;
switch (number)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
}
Output:
Two
The default label is optional. It is used when none of the
case values matches the switch expression.
int choice = 5;
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Invalid choice");
}
Since there is no case 5, the default block executes.
#include <stdio.h>
int main()
{
int choice;
printf("Enter a number from 1 to 3: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("You selected One");
break;
case 2:
printf("You selected Two");
break;
case 3:
printf("You selected Three");
break;
default:
printf("Invalid choice");
}
return 0;
}
A character can be used as the switch expression.
char grade = 'A';
switch (grade)
{
case 'A':
printf("Excellent");
break;
case 'B':
printf("Very Good");
break;
case 'C':
printf("Good");
break;
default:
printf("Invalid grade");
}
int day = 3;
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
default:
printf("Invalid day");
}
Each case represents a different day number.
int choice = 2;
switch (choice)
{
case 1:
printf("Add Student");
break;
case 2:
printf("View Student");
break;
case 3:
printf("Delete Student");
break;
default:
printf("Invalid choice");
}
Switch statements are commonly used for menu-based programs.
int a = 20;
int b = 5;
char op = '+';
switch (op)
{
case '+':
printf("%d", a + b);
break;
case '-':
printf("%d", a - b);
break;
case '*':
printf("%d", a * b);
break;
case '/':
if (b != 0)
{
printf("%d", a / b);
}
else
{
printf("Cannot divide by zero");
}
break;
default:
printf("Invalid operator");
}
int choice = 1;
switch (choice)
{
case 1:
printf("Welcome\n");
printf("You selected option 1\n");
break;
case 2:
printf("You selected option 2");
break;
}
A case can contain multiple statements.
Multiple case labels can share the same block of statements.
char grade = 'A';
switch (grade)
{
case 'A':
case 'B':
printf("Good grade");
break;
case 'C':
printf("Average grade");
break;
default:
printf("Other grade");
}
Both A and B reach the same statement.
If a case does not contain a break, execution continues into
the next case. This is called fall-through.
int number = 1;
switch (number)
{
case 1:
printf("One\n");
case 2:
printf("Two\n");
break;
}
For number = 1, both messages are printed because the first
case has no break.
Fall-through can be intentionally used when several cases should perform the same operation.
int month = 1;
switch (month)
{
case 1:
case 2:
case 3:
printf("First Quarter");
break;
default:
printf("Other Quarter");
}
Cases 1, 2, and 3 all reach the same block.
Both switch and if-else can be used for decision
making, but they are useful in different situations.
A switch is convenient when comparing one expression with several discrete constant values.
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
}
An if-else structure is more flexible for ranges and complex
conditions.
A switch is designed for matching case values, not for directly expressing
conditions such as marks >= 80.
For example, range-based grading is naturally written using
if-else:
if (marks >= 80)
{
printf("A");
}
else if (marks >= 60)
{
printf("B");
}
This is generally more suitable than trying to use a switch for ranges.
int number = 10;
switch (number)
{
case 5:
printf("Five");
break;
case 10:
printf("Ten");
break;
case 15:
printf("Fifteen");
break;
default:
printf("Other number");
}
Integer expressions are commonly used with switch statements.
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY
};
enum Day day = TUESDAY;
switch (day)
{
case MONDAY:
printf("Monday");
break;
case TUESDAY:
printf("Tuesday");
break;
case WEDNESDAY:
printf("Wednesday");
break;
}
Enumeration constants can also be used as case labels.
A case label must use a constant expression that is valid for the switch selection.
int number = 2;
switch (number)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
}
Case labels are not normally written using runtime variables such as
case x: when x is an ordinary variable.
A common beginner mistake is forgetting break.
switch (choice)
{
case 1:
printf("One");
case 2:
printf("Two");
break;
}
If choice is 1, both messages can be printed because execution falls through into case 2.
The same case value should not be repeated in one switch statement.
switch (number)
{
case 1:
printf("One");
break;
/* case 1 again would be invalid */
}
Each case value must be unique within the same switch statement.
The controlling expression of a traditional C switch should have an integral or enumeration type.
int choice = 2;
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
}
For floating-point range comparisons, an if-else structure is
usually the appropriate choice.
A switch case can contain an if statement when additional
conditions are required.
int choice = 1;
int age = 20;
switch (choice)
{
case 1:
if (age >= 18)
{
printf("Adult");
}
else
{
printf("Minor");
}
break;
default:
printf("Invalid choice");
}
#include <stdio.h>
int main()
{
int choice;
printf("1. Add\n");
printf("2. View\n");
printf("3. Update\n");
printf("4. Delete\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Add selected");
break;
case 2:
printf("View selected");
break;
case 3:
printf("Update selected");
break;
case 4:
printf("Delete selected");
break;
default:
printf("Invalid choice");
}
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
char op;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
switch (op)
{
case '+':
printf("Result = %d", a + b);
break;
case '-':
printf("Result = %d", a - b);
break;
case '*':
printf("Result = %d", a * b);
break;
case '/':
if (b != 0)
{
printf("Result = %d", a / b);
}
else
{
printf("Cannot divide by zero");
}
break;
default:
printf("Invalid operator");
}
return 0;
}
#include <stdio.h>
int main()
{
int day;
printf("Enter day number: ");
scanf("%d", &day);
switch (day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid day");
}
return 0;
}
Use a switch when:
Use if-else when you need ranges, complex logical conditions,
or comparisons that are not naturally represented by fixed case values.
Practice the following programs:
Always remember to use break when you want each case to execute
independently. Practice intentional fall-through separately.
switch statement is used for multiple fixed choices.case label.break statement normally stops execution from falling into the next case.default case handles unmatched values and is optional.break can cause fall-through.if-else is generally more suitable for ranges and complex conditions.Question: Which keyword is commonly used to stop execution from continuing into the next case of a switch statement?