Switch Case if conditions in Csharp

This code below will return well done because the condition is true to case 2

            int roll = 2;
            switch (roll)
            {

                case 1:
                    {
                        Console.WriteLine("Excellent");
                        break;
                    }

                case 2:
                    {
                        Console.WriteLine("Well Done");
                    }
                    break;

                case 3:
                    {
                        Console.WriteLine(" Fair");
                    }
                    break;


                case 4:
                    {
                        Console.WriteLine("Repeat");
                    }
                    break;


                case 5:
                    {
                        Console.WriteLine("Quiet");
                    }
                    break;

                default: Console.WriteLine("Invalid grade");
                    break;
            }
            Console.ReadLine();

A variable is switched so that the case can identify it 

 A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

           char grade = 'A';
            switch(grade)
            {

                case 'A':
                    {
                        Console.WriteLine("Excellent");
                        break;
                    }

                case 'B':
                    {
                        Console.WriteLine("Well Done");
                    }
                    break;

                case 'C':
                    {
                        Console.WriteLine(" Fair");
                    }
                    break;


                case 'D':
                    {
                        Console.WriteLine("Repeat");
                    }
                    break;


                case 'F':
                    {
                        Console.WriteLine("Quiet");
                    }
                    break;

                default: Console.WriteLine("Invalid grade");
                    break;
            }
            Console.ReadLine();

You can decide to use curry braces or not while ending your cases.. recommended for code readability.

The break statement in C# has following two usage: When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s