- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - nested switch statement in C
![]() Share with a Friend |
C Programming - nested switch statement in C
Nested switch Statement in C
A nested switch statement in C refers to placing one switch statement inside another switch statement. This allows for checking multiple conditions at different levels and is useful when you have complex decision-making requirements.
The inner switch statement will only be evaluated if the condition in the outer switch statement matches. Just like the regular switch statement, each switch block can have case, break, and default statements.
Syntax of Nested switch Statement:
C
switch (expression1) {
case value1:
// Code for case value1
switch (expression2) {
case value2:
// Code for case value2
break;
case value3:
// Code for case value3
break;
default:
// Code if expression2 doesn't match any case
}
break;
case value4:
// Code for case value4
break;
default:
// Code if expression1 doesn't match any case
}
- The outer switch checks expression1.
- If a case is matched, the inner switch checks expression2.
- The inner switch executes based on expression2's value if the outer switch condition is true.
Example: Nested switch Statement
C
#include <stdio.h>
int main() {
int outer = 2, inner = 3;
switch (outer) {
case 1:
printf("Outer case 1\n");
break;
case 2:
printf("Outer case 2\n");
switch (inner) {
case 1:
printf("Inner case 1\n");
break;
case 2:
printf("Inner case 2\n");
break;
case 3:
printf("Inner case 3\n");
break;
default:
printf("Inner default case\n");
}
break;
case 3:
printf("Outer case 3\n");
break;
default:
printf("Outer default case\n");
}
return 0;
}
Explanation:
- The outer switch checks if the outer variable is 1, 2, or 3.
- In this case, outer is 2, so it matches case 2.
- Inside the outer case 2, a second switch is executed, which checks the value of the inner variable.
- Since inner is 3, it matches case 3 in the inner switch, and "Inner case 3" is printed.
Output:
Outer case 2
Inner case 3
Example 2: More Complex Nested switch
C
#include <stdio.h>
int main() {
int outer = 3, inner = 1;
switch (outer) {
case 1:
printf("Outer case 1\n");
break;
case 2:
printf("Outer case 2\n");
switch (inner) {
case 1:
printf("Inner case 1\n");
break;
case 2:
printf("Inner case 2\n");
break;
default:
printf("Inner default case\n");
}
break;
case 3:
printf("Outer case 3\n");
switch (inner) {
case 1:
printf("Inner case 1\n");
break;
case 2:
printf("Inner case 2\n");
break;
default:
printf("Inner default case\n");
}
break;
default:
printf("Outer default case\n");
}
return 0;
}
Explanation:
- The outer switch checks if outer is 1, 2, or 3.
- Since outer is 3, the program enters case 3.
- Inside this, another switch is executed for the inner variable.
- Since inner is 1, it matches case 1 in the inner switch, and "Inner case 1" is printed.
Output:
Outer case 3
Inner case 1
Key Points about Nested switch Statements:
- Nested Structure: A switch can be nested inside another switch, making it useful for situations where decisions depend on multiple variables.
- Control Flow: The inner switch only gets evaluated when the corresponding outer switch case is matched.
- Case Matching: If multiple switch cases match, the code block associated with the first matching case is executed. All the following cases are skipped unless break is not used.
- Fall-through: Just like a regular switch, if there is no break statement, the program will continue executing the next case statements. However, this is rarely used in nested switch statements.
- Default Case: Both the outer and inner switch blocks can have a default case, which is executed if no other cases match.
Summary:
- A nested switch allows multiple layers of decision-making based on different conditions.
- The outer switch checks the first condition, and if it matches, it checks for inner conditions through another switch.
- This structure is helpful for handling complex decision-making processes with multiple related conditions.
