Developing Windows 7.5 Mango Apps, Part 6: Switch Statement

As we have already learnt in previous chapters, we can control flow of application execution based on certain conditions. There are a number of statements which can be used for the same purpose. We have seen in Part 4 that we can use “if statement” for testing user’s input against various conditions. We also learnt about Operators, expressions, statements in Part 5.

In this chapter we will learn more about one statement which can be used as an alternative to “if statement” and conditional operator equivalent to “if statement”.

The “switch statement” is a flexible alternate for “if statement” but only if we need to test a number conditions. The use of “switch statement” helps us to bypass blocks of codes for which value does not satisfy the condition. We will create an example project for you to understand it perfectly.

You can open our old application developed in “if statement” or else you can even download the project I have created for this tutorial. Now I hope that you have opened our old project or have downloaded my project. As usual double-click on the “Submit” button to go to the click event of that button. We will start with conditional operator instead the previous “if statement” for same example. Write following code in the button_click event.

interface

string message = (txtInput.Text == "1") ? "Boat" : "Car";
txtResult.Text = "You won a " +message;

Yes, that’s it. Now try running the program and check if it is working. Working pretty same as before? This is the power of (?:) operators.

When we want to check a condition and based on outcome show the result, we can use these two operators together to form the effect of “if-else statement”. In the first bracket we have defined our condition (to check input from user whether it is equal to 1 or not) preceded by a “?”. If it is true then the first value preceding “?” will be stored in our string variable but if the expression results in false, value preceding “:” will be stored. You can see, how many keystroke it can save if we use it properly!

Now comment out this code and let us turn towards “switch statement”. Copy the following code and paste it in button_click event as you did before.

string message = "";
            switch (txtInput.Text)
            {
                case "1":
                    message = "You won a new car!";
                    break;
                case "2":
                    message = "You won a new boat!";
                    break;
                case "3":
                    message = "You won a new cat!";
                    break;
                default:
                    message = "Sorry, we didn't understand your response.  You lose.";
                    break;
            }
            txtResult.Text = "You won a " +message;

Now try running the program. You will find that this program works just the same as previous. This is the functionality of switch statement.

app-runtime

When we have few conditions to check, use of conditional operator or “if statement” is recommended but when the situation arises where you have to check number of potential values, use of multiple if statements will complicate the program. Thus “switch statement” is designed solely for this purpose. It checks a condition for number of cases. Each case has possible value attached to it against which the case is tested. If the value matches to the condition, the code of block in that case is executed and rest is omitted.

If it happens that programs cannot find any case value matching the condition, default statement works as final “else” statement and code of block preceding “default” will be executed.

One thing should be noted here and that is “break statement” after each case. It helps program to break out of “switch” after a case is executed.

You can find all about a “switch statement” here.

So now you have 3 weapons in your arsenal to control program based on decisions, master all of them till we return with next tutorial, where we will learn about “for iterations and for loop”.

Posted by on , in Category General with Tags
Gadget freak, Apple lover, Windows Phone lover; well that’s me! I have been in this wonderful field of freelance writing for quite some time and looking forward to developing more illuminating content related to gadgets and technology, as I go along.

One Comment

  1. pushups

    why dont u give the details of the next tutorials?

    its written so swiftly dat v expect it more…..

Leave a Reply

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


9 + 5 =