This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

THE SWITCH STATEMENT IN C#
(1 vote, average: 3.00 out of 5)
Written by Christopher   
Thursday, 29 May 2008 16:01

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 (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options