Developing Windows 7.5 Mango Apps, Part 4: if statement

In previous chapters of this series, we started off with very basic things like introduction, first small application and we also learnt some other basic concepts like variables and assigning values to them.

In this chapter, things will get a little more interesting. In last chapter we only received input from user to show him some output. We will learn to make some decisions based on the input received from user.

The syntax of the “if” statement is as follows:

If (condition)
            Lines of code;

It is called as decision-making statement because based on conditions that we set up, application decides whether to execute a particular block of code or not.

Let us start off with creating new project. Create a new project by any name that suits you. For the sake of convenience, I have named my project ‘ifstatement’.

Now drag a text block (txtDescription) to the lower side of page spanning complete emulator screen in width (See image). Below that pull one text box (txtInput) and one button (btnSubmit) and drop them beside each other. At the last pull another text block (txtResult) and make it a little large in both height as well as width. Now make all controls except button blank, i.e. remove text from their text property while change text of button to “Submit”. You can add text in first text block if you wish. I have added few guidelines for our little games/app (let it be any stupid). Now our interface is ready and looks like I have shown in image.


Now double-click on Submit button to go to button_click event in C# window. In that event (in those two curly braces containing click event), write following line of codes, just to check basic functionality of our app.

string userValue = txtInput.text;
if(userValue == “1”)
txtResult.text = “Congrats you won $1000”;

Now run the project. After entering 1 in text box press “Submit”; you will get line as “Congrats you won $1000”. But if you try entering anything else than 1 in text block before submitting, nothing will happen as we have not told our application what to do if condition is false.

This block is simplest form of “If” statement. There are several other forms of “if” statement which can be used in evaluating multiple conditions, namely “if-else”, “else-if”, “nested if’s”. Now open C# part of our program after stopping debugging mode and write following code there. This code will demonstrate use of “else-if” loop.

                string userValue = txtInput.Text;
                if (userValue == "1")
                    txtResult.Text = "Congrats you won $1000";
                else if (userValue == "2")
                    txtResult.Text = "Better luck next time!";
                else if (userValue == "3")
                    txtResult.Text = "You gotta try hard!";
                    txtResult.Text = "Nah enter properly.";

This is called as “else-if” ladder. It tests value against several conditions and executes the respective block of code.


Two things should be noted here, in “else-if” ladder last “else” statement is not associated with any if statement and other is “=” operator is not same as “==”operator. “=” operator assigns value to L.H.S. while “==” is used for comparing values. Nested “if’s” and “if-else” are nothing but variations of “simple if” and “else-if” ladder, which you can attempt in the same project easily.

So until we return with the next chapter, master this “if” statement completely.

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.


  1. A XVI

    I would like to know the difference between “name” and “UserValue” .. I tried both in the same place and they do the same thing.

  2. A XVI

    The next chapter isn’t done yet or what? ://

Leave a Reply

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

6 + 9 =