Friday 17 August 2018

C#: Switch statement

switch statement evaluates its expression, then executes all statements that follow the matching case label. The body of a switch statement is known as a switch block. A statement in the switch block can be labelled with one or more case or default labels.

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter a Number");

        int number = int.Parse(Console.ReadLine());

        switch (number)
        {
            case 1:
                Console.WriteLine("Sunday");
                break;
            case 2:
                Console.WriteLine("Monday");
                break;
            case 3:
                Console.WriteLine("Tueday");
                break;
            case 4:
                Console.WriteLine("Wednesday");
                break;
            case 5:
                Console.WriteLine("ThursDay");
                break;
            case 6:
                Console.WriteLine("Friday");
                break;
            case 7:
                Console.WriteLine("Saturday");
                break;
            default:
                Console.WriteLine("You entered wrong day number");
                break;
        }
    }
}

Sample Output
Enter a Number
5
ThursDay

Sample Output
Enter a Number
15
You entered wrong day number




Previous                                                 Next                                                 Home

No comments:

Post a Comment