Q:

C# program to demonstrate example of nested switch statement

0

Nested switch statement

switch statement in C# allows checking a variable/value with a list of values (cases) and executing the block associated with that case.

When we use switch statement within another switch statement (a case statement(s)) i.e. switch statement within another switch statement, we can say it is an example of a nested switch statement.

Synatx:

    //outer switch
    switch(variable/expression)
    {
        case <case_value1>:
            statement(s);
            //inner switch
            switch(variable/expression)
            {
                case <case_value1>:
                    statement(s);
                    break;
                case <case_value2>:
                    statement(s);
                    break;
                default:
                    statement(s);
                    break;
            }
        break;
        case <case_value2>:
            statement(s);
            break;
        default:
            statement(s);
            break;
    }

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C# code for nested switch statement

Here, we have 3 cases:
(Case 1) Using another switch statement, that will give the color name based on the user input (color code – example "R/r" for "Red", "G/g" for "Green", ...)
(Case 2) and Case 3) will print simple message.

// C# program to demonstrate example of nested switch statement
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int number;

            //input a number
            Console.Write("Enter a number (1-3): ");
            number = Convert.ToInt32(Console.ReadLine());

            //outer switch statement
            switch (number)
            {
                case 1:
                    //using another case 
                    //it will input R,G or B and print the color
                    char color;
                    Console.Write("Enter color value (R/G/B): ");
                    color = Console.ReadLine()[0];
                    //validating it using switch case
                    //inner switch
                    switch (color)
                    {
                        case 'R':
                        case 'r':
                            Console.WriteLine("You've choesn "Red" color");
                            break;
                        case 'G':
                        case 'g':
                            Console.WriteLine("You've choesn "Green" color");
                            break;
                        case 'B':
                        case 'b':
                            Console.WriteLine("You've choesn "Blue" color");
                            break;
                        default:
                            Console.WriteLine("Invalid color code");
                            break;
                    }
                    break;

                case 2:
                    Console.WriteLine("Input is 2");
                    break;

                case 3:
                    Console.WriteLine("Input is 3");
                    break;
                default:
                    Console.WriteLine("Invalid number");
                    break;
            }


            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

First run:
Enter a number (1-3): 1
Enter color value (R/G/B): B
You've choesn "Blue" color

Second run:
Enter a number (1-3): 1
Enter color value (R/G/B): r
You've choesn "Red" color

Third run:
Enter a number (1-3): 1
Enter color value (R/G/B): x
Invalid color code

Fourth run:
Enter a number (1-3): 3
Input is 3

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now