Hello, hello and yeah hello to you too! Welcome back to a brand new post in this series of Windows Phone 7 Development tutorial. As stupid as it may look like, any simple app you will try to create on your own is gonna be a really good learning curve for you. Even Da Vinci did thousands of paintings before he actually painted his masterpieces, which are countless. But the fact remains, every great man started as nothing. So do keep practicing, doesn’t matters if your friends think you are kinda a nutcase now, they are probably very insecure of your development!
Suggested Reading :
- Beginner’s Guide to Windows Phone 7 App development
- Create Hello World Application in Windows Phone 7
- Different properties of Windows Phone 7 emulator
- Coding the Hello World Application in Windows Phone 7
- How files interact in Windows Phone 7 App development
- Declaring Variables in Windows Phone Application Development
- Accepting input & displaying it in Windows Phone Application Development
Ok so enough of advices; let me concentrate on why I am here today. Today we will deal with a kinda of statement which is very interesting. This is called the if statement. There are even various types under this heading so you might wanna gear up for a long day at work!
Check me and do me else go to him-
1. We have been writing some really simple codes which are kinda boring you know. They execute in the same pattern line-after-line and one
after the other. Now the statement we are going to learn today is gonna take an input from the user (or we can initialize it too) then it
is going to check the condition inside its brackets “()”, (we call this checking) and if the condition/s is/are satisfied, then the
statements inside its body is executed. That must be quite confusing. Let me draw you to a real-time example to simple things out.
“Let’s take the example that you are very hungry at the moment. But the problem is that you don’t know how to cook, and neither there is
any food left in your house. So what do you do? Go to a restaurant or a fast-food joint and eat something. But before you go into the
restaurant and eat, you must check whether you have any money or not, as you won’t be able to pay the bill if you don’t have.
And that may make things even more complicated for you! If you have money in your pocket, then you go and eat your fill, else you come
out of the restaurant. Similar to this we have the if statement.
The money-checking action is checking whether the value inside the brackets match with the input value or not. If it matches, continue,
else come out.”
2. I will give you a little syntax for the if statement-
So here, if the keyword we are gonna use. This keyword has another element, the condition/s inside its brackets which will check
if condition/s is/are satisfied or not. If the condition/s is/are satisfied, then the computer will execute the job/s we have assigned inside
the statement body. Once completed, it comes out of the statement.
3. An example will nail it down for you. We will build a simple app which will check if I have won a prize or not, according to our input. I will
start by showing you just the design page and then will directly go to the coding part as you would have
developed enough experience doing it by now-
This is going to be the look of our app.
4. Let us double click the button and program it. Take a good look at the code-
So according to our code, if the user will luckily enter ‘1’ as an option, the computer is going to display ‘You have won a car!!’. This means
unless the user enters ‘1’ as the option, the computer is not going to execute the statement. Let’s try to run the code-
So as we can see that if we enter the input 1, we will get the output message displayed.
5. The drawback with this code, as you may have very well noticed, is that the app will not do anything if the input is something other than
1, which is going to confuse the user. So let’s try to improve our app again by improving on our code.
We will use an offshoot of the if statement called the if-else statement. This statement is going to check the other input conditions from
the user and will give an output accordingly.Let’s implement it in our code,
If you will have noticed, we have introduced a new keyword called, ‘if else’ which will check for other input conditions, in case the condition
does not satisfies the ‘if’ statement. Finally, we have an ‘else’ key word which will check take care of any other condition which the user
gives and doesn’t satisfies all the other statements.
6. Let’s run the code and see if it works or not-
As expected, any kind of input from the user can be handled and this has really improved the quality of our app.Let’s continue next time
around with some more wonderful statements, conditions and apps! Bye for now!!