Hey folks!! Has been quite a while since I blogged last time. Well, I was kinda busy with my own dreamy world and I couldn’t grab an opportunity to actually run into doing content for you, which is quite a shame! Well, I am back and hopefully we will be having lots of fun with the new tool that I have in store for you today. We will be dealing with ‘iterations’ today. Now its kinda new for you and I would suggest that you do a good double-take at the description and visuals I have in store for you. Let’s get started.
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
- Using If Else Statement in Windows Phone 7 App Development
- How to create a Calculator App for Windows Phone 7
- Switch statement in Windows Phone 7 App development
Visuals might just be the line between you and the app!
Do it till you are broke!
1. The statement we are going to talk about today is called the ‘for’ loop or the for iteration. It’s a very useful tool when we build apps as sometimes we do need to skip some steps which are not going to help our cause. Taking that in mind let me give you a simple syntax of how the for loop looks like-
for (<initialization>; <condition/s>; <increment/decrement>)
So as you can clearly see, this statement is really beautiful! Now I will go on with explaining each part of the statement-
for-this is the keyword we will be using every time we will want to use this iteration.
<initialization>-the for loop cannot work very well if we will not initialize the variable we are using. It gives the computer an idea of ‘where’ to start from.
<condition/s>-this consists of the ‘limitations’ or the limit to which you want the iteration to continue.
<increment/decrement>-this deals with increasing the value of the variable so that we have ‘better control’ of how the statement loop is going to work like.
2. We might even drop in a real-time example to make matters easier-
“Have you ever cleaned your room?! Now for those of you who don’t know what I am talking about, it requires you to ‘arrange’ things in order. If the condition is like your mother is going to check your room until its sparkling clean, you might have to do the scrubbing and rubbing process again and again. This can be stated in a for loop form in the following way-
for (room=unclean; room!=sparkling-clean; rub-and-arrange++)
That would be the for loop for the room-cleaning part! If you see the statement, you will realize what you have to do. From the first part, you can see that your room is at the ‘unclean’ state at the moment. So you initialize it as room=unclean. Now from the next part, we can see that the condition states, until ‘room is not clean’. That means you have to continue cleaning till its sparkling clean! Then the last part states the do it as many times until the room is clean part, or the increment the cleaning ‘rub-and-arrange’ part again and again!! Now your room is clean!”
3. All that said, it’s time for my favorite part-building the app from the statement. I have explained you how to design the interface of the app and so I will just give you a quick view of how the app is going to look like. It’s the same ‘play’ app we built last time-
4. Double click the play button and let’s do the coding part as follows-
Take a good look at the code. The value of i variable is initialized to the choice variable. Then we set the value of I to 1 and give a condition so that its value is less than 3. Then we increment it after that. Inside the for loop we have a nested ‘if-else’ statement which means that this statement is written ‘inside’ the for loop.
5. Let’s try to run the app and see what we get as an output-
6. Let’s give a choice greater than 3-
So we can see that as we had set the if-else condition to get a prized output for only choices ranging from 1 to 3, when we give an input choice of numbering greater than 3, we get the following message.
Try to improve on the app by giving even more interesting conditions. Hope you will have fun doing that. That’s all for this time, I will see you with even more cool stuffs next time around!