Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Explain nested switch case in C language
Nested switch case in C allows you to place one switch statement inside another switch statement. This technique is useful when you need to perform multi-level decision-making based on different variables or conditions.
Syntax
switch (outer_expression) {
case value1:
switch (inner_expression) {
case inner_value1:
// statements
break;
case inner_value2:
// statements
break;
default:
// default statements
}
break;
case value2:
// statements
break;
default:
// default statements
}
Key Rules
- The inner switch statement is completely independent of the outer switch
- Case values of inner and outer switch can have common values without conflict
- Each case must end with a colon (:) and include a break statement
- Both outer and inner switches can have their own default cases
- Proper indentation improves readability of nested structures
Example: Password Validation System
This example demonstrates a simple authentication system using nested switch statements −
#include <stdio.h>
int main() {
int userid;
int password;
printf("Enter User ID: ");
scanf("%d", &userid);
switch (userid) {
case 1234:
printf("Enter Password: ");
scanf("%d", &password);
switch (password) {
case 5678:
printf("Login Successful! Welcome to TutorialsPoint<br>");
break;
case 0000:
printf("Using default password. Please change it.<br>");
break;
default:
printf("Incorrect password!<br>");
break;
}
break;
case 9999:
printf("Enter Admin Password: ");
scanf("%d", &password);
switch (password) {
case 1111:
printf("Admin access granted!<br>");
break;
default:
printf("Invalid admin password!<br>");
break;
}
break;
default:
printf("Invalid User ID!<br>");
break;
}
return 0;
}
Enter User ID: 1234 Enter Password: 5678 Login Successful! Welcome to TutorialsPoint
Example: Menu-Driven Calculator
Here's another example showing nested switch for a calculator with operation subcategories −
#include <stdio.h>
int main() {
int choice, subChoice;
float a = 10.0, b = 5.0, result;
printf("Calculator Menu:<br>");
printf("1. Basic Operations<br>2. Advanced Operations<br>");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Basic Operations:<br>");
printf("1. Addition<br>2. Subtraction<br>");
printf("Enter operation: ");
scanf("%d", &subChoice);
switch (subChoice) {
case 1:
result = a + b;
printf("%.2f + %.2f = %.2f<br>", a, b, result);
break;
case 2:
result = a - b;
printf("%.2f - %.2f = %.2f<br>", a, b, result);
break;
default:
printf("Invalid basic operation!<br>");
break;
}
break;
case 2:
printf("Advanced Operations:<br>");
printf("1. Multiplication<br>2. Division<br>");
printf("Enter operation: ");
scanf("%d", &subChoice);
switch (subChoice) {
case 1:
result = a * b;
printf("%.2f * %.2f = %.2f<br>", a, b, result);
break;
case 2:
result = a / b;
printf("%.2f / %.2f = %.2f<br>", a, b, result);
break;
default:
printf("Invalid advanced operation!<br>");
break;
}
break;
default:
printf("Invalid menu choice!<br>");
break;
}
return 0;
}
Calculator Menu: 1. Basic Operations 2. Advanced Operations Enter your choice: 1 Basic Operations: 1. Addition 2. Subtraction Enter operation: 1 10.00 + 5.00 = 15.00
Conclusion
Nested switch statements provide a structured way to handle complex multi-level decision-making in C. They improve code organization and readability when dealing with hierarchical menu systems or multi-step validation processes.
Advertisements
