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

C# program to demonstrate example of switch statement

C# switch statement example: Here, we are going to learn about the switch case statement program/example.
Submitted by IncludeHelp, on April 09, 2019

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

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





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.