Q:

C# program to demonstrate example of switch statement

0

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

Syntax:

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

Note:

  • default case is an optional - but it should be used, because no case value is matched then default statements will be printed.
  • “break” is an optional but if we are testing multiple case values together. Like,
        case <case_value1>:
        case <case_value2>:
            statement(s);
            break;
    
  •  

All Answers

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

C# code to demonstrate example of switch statement

Here, we are asking for a day number from the user between the range of 0 to 6, and printing day name (example: 0 for Sunday, 1 for Monday and so on...).

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

namespace IncludeHelp
{
    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

Here, we are asking for a character from the user – and checking 1) input character is an alphabet using if-else statement and if it is an alphabet – we are checking for vowels & consonants.

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

namespace IncludeHelp
{
    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

 

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

total answers (1)

C# program to demonstrate example of nested switch... >>
<< C# program to demonstrate example of nested condit...