Text Size

THE SWITCH STATEMENT IN C#

PDF 

Tutorials

(1 vote, average: 3.00 out of 5)

INTRODUCTION

The switch statement offers the programmer the capability to choose a specific block of code from a set of mutually exclusive ones. It is similar but not identical to the if statement.  The block of code that will be chosen is not known before the execution of the program. This can be for example due to different windows platform or specific localization settings of each country.

 

SYNTAX AND USE

The syntax of this statement is similar to the statement select case in Visual Basic. To use such a statement you include an argument that will be checked. Then, every case inside the switch statement is checked against this argument. When the argument evaluates to one of these cases the code following the particular case is executed. Contrary to statements like if and while, the end of the code block for each case is determined by putting a break statement in the end, not with curly braces({}). The following snippet of code provides an illustrative example for this statement:

 

  static void Main(string[] args)

        {

            string myInput;

            myInput = Console.ReadLine();

            switch (myInput)

            {

                case "Start":

                    Console.WriteLine("You have written the word Start");

                    break;

                case "Middle":

                    Console.WriteLine("You have written the word Middle");

                    break;

            }

        }      

 

Try this code and see what it does. It is possible to add a default case that will execute if no other case that matches the argument of the statement can be found:

 

       

        static void Main(string[] args)

        {

            string myInput;

            myInput = Console.ReadLine();

            switch (myInput)

            {

                case "Start":

                    Console.WriteLine("You have written the word Start");

                    break;

                case "Middle":

                    Console.WriteLine("You have written the word Middle");

                    break;

                default:

                    Console.WriteLine("You have written something else");

                    break;

            }

        }      

  

In our  first example if the user writes anything but the strings in the two cases, nothing will happen. In the second example the default case is triggered in such a case and a default message is written on the screen. You can make two cases execute the same block of code with this tecnhique:

 

        static void Main(string[] args)

        {

            string myInput;

            myInput = Console.ReadLine();

            switch (myInput)

            {

                case "Start":

                case "End":

                    Console.WriteLine("You have written either Start or End");

                    break;

                case "Middle":

                    Console.WriteLine("You have written the word Middle");

                    break;

                default:

                    Console.WriteLine("You have written something else");

                    break;

            }

        } 

 

Bear in mind that the order of the cases is not important. The statement will not execute the one found first but the one that exactly matches the argument. If non is found, the default case will execute. You can even put the default case first. However, you can’t have two cases with the same value, and this includes constants. Also, the values cannot contain variables. Instead they must be constant expressions.

Trackback(0)
Comments (1)add comment

NaMeLeSs said:

0
...
smilies/smiley.gif thx, very helpful!!!
 
January 29, 2009
Votes: +1

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1