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

Spread the love

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

Leave a Reply

Your email address will not be published. Required fields are marked *