Menu
Coddy logo textTech

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 icon

Challenge

Medium

Create a method called calculatePrice that:

  1. 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
  2. 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

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
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow