C#

C#- Programming Guide | Starting Phase Learning(10)

Here, we will discuss the other conditional branching statement in c#

switch- case statements in C#:

It is a conditional selection statement where from the list of expressions(case)statements, it chooses the one with the matched switch section.The switch statement is an alternative to an if-else construct if a single expression is tested against three or more conditions
Syntax goes like:

switch(expression) {
case (expression1) :
statement(s);
break;
case (expression2 ) :
case (expression3 ) :
statement(s);
break;
default : //It’s optional
statement(s);
}

Working:
1.Switch (Expression)- it starts the operation where it comprises different case statements which acts as like an if , else-if .
2. The value of the expression in the switch is compared with the values of each case
3.The break statement is used to break the loop.
4.If there is a match(condition becomes true), the associated block of code gets executed
5. The switch statement contains multiple case labels.

Switch case operation in c#
Output

public static void Mystudent()
{
string _key;
Console.WriteLine(“Please key in a to perform addition”);
Console.WriteLine(“Please key in s to perform substraction”);
Console.WriteLine(“Please key in d to perform division”);
Console.WriteLine(“Please key in m to perform multiplication”);
int a = 10;
int b = 5;
int c;
_key = Console.ReadLine();

Explanation:

        switch (_key) //Starting the switch expression
        {
            case "a":   //similiar like if(key == "a")
                c = a + b;
                Console.WriteLine("Result is"+c);
                break; //end the loop
            case "s":
                c = a - b;
                Console.WriteLine("Result is" + c);
                break;
            case "d":
                c = a / b;
                Console.WriteLine("Result is" + c);
                break;
            case "m":
                c = a * b;
                Console.WriteLine("Result is" + c);
                break;
            default:// if all cases are false then statements written under default gets executed
                Console.WriteLine("You have entered a wrong choice");
                break;

        }

    }

switch (_key)://Starting the switch expression
case “a”: //similiar like if(_key == “a”)
break; //ends the loop
case “s”://Its like else-if (_key == “s”)
default: //similiar like an else statement , if none gets executed then the statement written under it gets executed.

Nested switch:

Nested switch statments are allowed in C# by writing inner switch statement inside a outer switch case.

int a =Convert.toInt16( Console.ReadLine());
switch (a)// 1st switch
{
    case 1:
        Console.WriteLine("Welcome to tenOclocks.com");
        switch (a++) //2nd switch (Nested switch)
        {
            case 1:
            Console.WriteLine(a);
            break;
            case 2:
            Console.WriteLine(a);
            break;

        }
        break;
    case 2:
        Console.WriteLine(++a);
     switch (a++) //2nd nested switch
        {
            case 1:
            Console.WriteLine(a);
          switch (a++)//3rd nested switch
           {
            case 1:
            Console.WriteLine(a);
            break;
            case 2:
            Console.WriteLine(a);
            break;

        }
            break;

            case 2:
            Console.WriteLine(a);
            break;

        }
        break;
    case 3:
        Console.WriteLine(++a);
        break;
    default:
        Console.WriteLine(++a);
        break;
}

tenocloc

Leave a Comment

Recent Posts

The Ultimate Guide to Choosing the Best Security Company in Noida

When it comes to security services in Noida, there are several reputable companies that cater to various needs. Let me…

6 months ago

Steel Bite Pro: The Revolutionary Supplement Transforming Oral Health Forever

Are you tired of dealing with tooth decay, gum problems, and expensive dental treatments? Look no further! Steel Bite Pro…

9 months ago

Captivating Elegance: Unveiling the Beauty of Oval Moissanite Rings

In the world of fine jewelry, oval moissanite rings have emerged as a captivating choice for those seeking elegance and…

10 months ago

Top 10 Digital Marketing Agency In Noida

Noida, located in the state of Uttar Pradesh, is a fast-growing hub of digital marketing agencies in India. Choosing the…

1 year ago

Two-Wheeled Thrills: The Ultimate Guide to the Latest Bikes in India 2023

As we move into 2023, the world of motorcycles in India is set to see some exciting changes. Manufacturers are…

1 year ago

Five new rules will change the game in IPL 2023

The Indian Premier League (IPL) is one of the most popular and exciting cricket tournaments in the world. With the…

1 year ago