Home » C# » C# programs » C# basic programs

C# program to demonstrate example of nested switch statement

C# nested switch statement example: Here, we are going to learn how to use nested switch statement (switch within switch) in C# programming language?
Submitted by IncludeHelp, on April 09, 2019

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;
    }

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.