Nested Switch Statements
Part of the Logic & Flow section of Coddy's C# journey — lesson 11 of 66.
Nested switch statements let you place one switch statement inside another for more complex decision-making.
Create a basic nested switch:
string department = "Sales";
string level = "Senior";
switch (department)
{
case "Sales":
switch (level)
{
case "Junior":
Console.WriteLine("Sales Junior");
break;
case "Senior":
Console.WriteLine("Sales Senior");
break;
}
break;
case "IT":
switch (level)
{
case "Junior":
Console.WriteLine("IT Junior");
break;
case "Senior":
Console.WriteLine("IT Senior");
break;
}
break;
}Always include break statements to prevent fall-through to the next case.
Remember to keep your code readable with proper indentation.
Challenge
MediumCreate a method called calculatePrice that:
- Takes three parameters:
- productType (string): Either "Electronics", "Clothing", or "Food"
- productTier (string): Either "Basic", "Premium", or "Luxury"
- basePrice (int): The starting price of the product
- Returns the final price (int) after applying these rules:
- For Electronics:
- Basic: Add $50 to base price
- Premium: Add $100 to base price
- Luxury: Add $250 to base price
- For Clothing:
- Basic: Add $10 to base price
- Premium: Add $50 to base price
- Luxury: Add $100 to base price
- For Food:
- Basic: No change to base price
- Premium: Add $15 to base price
- Luxury: Add $30 to base price
- For any other product type or tier, return the base price unchanged
- For Electronics:
Cheat sheet
Nested switch statements allow you to place one switch statement inside another for complex decision-making:
string department = "Sales";
string level = "Senior";
switch (department)
{
case "Sales":
switch (level)
{
case "Junior":
Console.WriteLine("Sales Junior");
break;
case "Senior":
Console.WriteLine("Sales Senior");
break;
}
break;
case "IT":
switch (level)
{
case "Junior":
Console.WriteLine("IT Junior");
break;
case "Senior":
Console.WriteLine("IT Senior");
break;
}
break;
}Always include break statements to prevent fall-through to the next case and maintain proper indentation for readability.
Try it yourself
public class CalculatePrice
{
// Implement the calculatePrice method
public static int calculatePrice(string productType, string productTier, int basePrice)
{
// Write your code here
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety