Advertisement

Latest Post

Sunday, 1 January 2017

How to demonstrate the use of switch case statement[C#]

Write a program in C# to demonstrate the use of switch case statement.

Following is the simple C# code :

class Lab
    {
        static void Main(string[] args)
        {

            char grade;
            Console.Write("Enter a grade : ");
            grade = Convert.ToChar(Console.ReadLine());            

            switch (grade)
            {
                case 'A':
                    Console.WriteLine("Excellent!");
                    break;
                case 'B':
                case 'C':
                    Console.WriteLine("Well done");
                    break;
                case 'D':
                    Console.WriteLine("You passed");
                    break;
                case 'F':
                    Console.WriteLine("Better try again");
                    break;
                default:
                    Console.WriteLine("Invalid grade");
                    break;
            }
            Console.WriteLine("Your grade is  {0}", grade);
            Console.ReadLine();

        }
    }

Output:-

Government Jobs