Developing Windows Phone Mango Apps, Part 7: for iterations

As a part of the Learn to Develop Windows Phone 7.5 Mango Applications, in our last tutorial we have seen that we can use “switch statement” if there are multiple values to be tested against one condition. In this tutorial we will learn “for statement” or better known as “for iterations” or “for loop”. Although there are multiple iteration statements available in C#, on our first day we will only master “for loop”.

for iterations or for loop

While writing any application, sometimes you need to iterate through one block of statements for number of times until you find successful match for your condition. This can be done using “for iteration”. This “for loop” might not seem useful at first glance, but trust me; you are going to need it for sure.


So let’s get started by creating a new project with some significant name like “for iterations”. Drag one button and one textblock onto the lower region of our emulator (see image) and name them properly (using convention we learned in the previous chapters). Now double-click on our only button to get into the button_click event.

Next paste the following code into the two curly braces of button_click event. Don’t worry, we will go through it once again so that you can understand.

String message ="";
            for ( int i = 0; i<10; i++)
                        message = message + i.ToString() + System.Environment.NewLine;


            myTextblock.Text = messege;

Now try running the application. I hope you have received output I have shown in the image.


Having done that, let us now understand each aspect of this application.

We have declared a string type variable “message” for using it within the loop. Then in “for” loop, we have initiated one temporary variable “i”, which keeps track of the number of iterations performed. The semi-colon preceding “i=0” indicates that we have finished with first thought and we are moving now towards next one. After the initiation, we have stated our condition. The condition tells compiler about how many times this loop should be executed. As soon as the condition gets satisfied, compiler jumps out of the loop.

After the condition, we have increased the value of variable. Here one thing should be noted that, instead “i++”, we can use “i+1”. So each time the “for loop” gets executed, value stored in string variable “message” gets increased. We have added this line (System.Environment.NewLine) to add new line character after every execution of loop.

There is another variation of this “for loop” known as “for-each loop”; but we will learn about it later. For now practice with “for loop”. Click here to go to the next chapter.

Posted by on , in Category Phone 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. alexandra murashova

    apps are hot today, everybody wants them, why hire developers? i used – it’s a very easy service for anyone.

  2. Ahmed A

    Very confusing. Will need to re-read this with more thought.

  3. someone

    yaar……… please add links to next & previous tutorials……… its hard to navigate

Leave a Reply

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

5 + 7 =