C# program to demonstrate example of switch statement

Syntax

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

C# code to demonstrate an example of the switch statement

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

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int day;
            
            //input the day number
            Console.Write("Enter day number (0-6): ");
            day = Convert.ToInt32(Console.ReadLine());
            
            //checking 
            switch (day)
            {
                case 0:
                    Console.WriteLine("Sunday");
                    break;
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                case 6:
                    Console.WriteLine("Saturday");
                    break;

                default:
                    Console.WriteLine("Invalid Input");
                    break;
            }

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

Output

First run:
Enter day number (0-6): 3
Wednesday

Second run:
Enter day number (0-6): 9
Invalid Input

C# code to check input character is a VOWEL or CONSOTANT using switch statement

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

namespace Justtechreview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            char ch;
            
            //input a character
            Console.Write("Enter a character: ");
            ch = Console.ReadLine()[0];
            
            //checking for valid alphabet
            if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
            {
                //checking for vowels
                switch (ch)
                {
                    case 'A':
                    case 'a':
                    case 'E':
                    case 'e':
                    case 'I':
                    case 'i':
                    case 'O':
                    case 'o':
                    case 'U':
                    case 'u':
                        Console.WriteLine("Input character {0} is a Vowel", ch);
                        break;

                    default:
                        Console.WriteLine("Input character {0} is a Consonat", ch);
                        break;
                }
            }
            else
            {
                Console.WriteLine("Input character {0} is not a valid alphabet", ch);
            }


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

Output

First run:
Enter a character: X
Input character X is a Consonat

Second run:
Enter a character: i
Input character i is a Vowel

Third run:
Enter a character: $
Input character $ is not a valid alphabet

Leave a Comment

error: Alert: Content is protected!!