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:
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 next chapter, master this “if” statement completely.