Friday, 24 September 2010

C# program for Switch Statement | Switch � Case Example

This is a switch program to demonstrate the use if switch statement in C#. We have used two cases together to show multiple cases for one options. in place of integer options you are allowed to use other data types.

C# program for Switch Statement | Switch – Case Example

C# program for Switch Statement | Switch – Case Example

Source Code of Switch Statement:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsIfElase
    {
        public static void Main()
        {
            try
            {               
                int InputNumber;
                Console.Write("Please enter a number: ");
                InputNumber = Int32.Parse(Console.ReadLine());              

                switch (InputNumber)
                {
                    case 1:
                    case 4:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    case 2:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    case 3:
                        Console.WriteLine("Number is {0}.", InputNumber);
                        break;
                    default:
                        Console.WriteLine("Number {0} is not between 1 and 4.", InputNumber);
                        break;
                }

            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment