C# switch

HTML
CSS
C#
SQL

Switch Case in C#

In C#, switch is a control statement that allows you to select one of many code blocks to be executed. It’s often used when you have a single expression (variable or value) that you want to compare against multiple possible values and execute different blocks of code based on which value matches the expression.

For Example :

Basic Structure :

switch (expression)

        {

            case value1:

                //code to be executed;   

                break;

            case value2:

                //code to be executed;   

                break;

                ……

            default:    

                //code to be executed if all cases are not matched;   

                break;

        }

Switch Case Statement

using System;

public class SwitchCaseExample

{

    public static void Main(string[] args)

    {

        int day = 3;

        string dayName;

 

        switch (day)

        {

            case 1:

                dayName = “Monday”;

                break;

            case 2:

                dayName = “Tuesday”;

                break;

            case 3:

                dayName = “Wednesday”;

                break;

            case 4:

                dayName = “Thursday”;

                break;

            case 5:

                dayName = “Friday”;

                break;

            case 6:

                dayName = “Saturday”;

                break;

            case 7:

                dayName = “Sunday”;

                break;

            default:

                dayName = “Invalid day”;

                break;

        }

 

        Console.WriteLine(“The day is: ” + dayName);

    }

}

Course Video

Practices Tasks

1.Write a C# program to simulate a simple calculator.

The program should prompt the user to enter two numbers and an arithmetic operation choice.

Prompt for first number after giving input click enter

2. Prompt for second number after giving input click enter

3. Display operation choice after giving input click enter

4. Performed selected operation if 1 do addition

5. if 2 do subtraction

6. if 3 do multiplication

7. if 4 do division

8. If 2 input contain value  0  when we doing division then it should show a below error

9. If we select operation out of 1to 4 then also it should give a below error