Control Structures – The Loop

Control Structures

When developing software, you most probably will need to make use of some form or another of iterative logic. Like most languages, C# offers a number of looping constructs each of which can have different uses.

The for Loop

This loop structure tends to be used when you know how many times you want to iterate, and it is also preferred if you want to keep a counter or an iterator.

The for loop is made out of three parts:

for(<initializer>; <boolean expression>; <iterator>) { <your code> }

The initializer is where you can declare and initialize a counter variable. This part of the loop is executed before the actual loop iterations, so it is a one time execution.

The boolean expression is a boolean value which is checked on each loop iteration. The loop will keep executing as long as the boolean expression results to true. When this value becomes false the loop will terminate.

The iterator usually consists of a counter being incremented or decremented. This is done on each loop iteration.

Below is an example of a for loop which is looping for 10 iterations and printing the iteration value to screen.

for (int i = 0; i < 10; i++)
    Console.WriteLine("Iteration: {0}", i);

The foreach Loop

This loop structure operates on collections of objects which implement the IEnumerable interface, such as the ArrayList for example.

The syntax for a foreach loop looks like this:

foreach (<type> <iteration variable>  in <list>) { <your code> }

The type is the data type of item contained within the list being iterated. For example,when iterating an ArrayList populated with string objects, the type would be string. If instead you ar iterating an array of integers the type would be int.

The iteration variable is used to access the current item in the loop, and the list is the collection of items you want to loop.

Below is an example of a foreach loop which is iterating an array of names and displaying each one on screen.

string[] names = { "Dave", "Francine", "Andrew", "Maria", "Keith" };

foreach (string name in names)
    Console.WriteLine("Hello {0}", name);

It is important to note that with the foreach loop the iteration variable (name in the above example) is read-only.

The while Loop

This loop structure is probably the simplest form of loop in C#. The syntax for this loop is as follows:

while (<boolean expression>) { <your code> }

The while loop keeps on iterating until the boolean expression results in false.

Below is an example of a while loop. The loop will iterate for 5 times and display the iteration number on screen.

int counter = 0;

while (counter < 5)
    Console.WriteLine("Iteration: {0}", counter);

The do-while Loop

A do-while loop and a while loop are very similar. The only difference is that the do-while loop checks its boolean condition at the end of the loop. This means that a do-while loop will always execute at least once since it checks if it should exit the loop at the end.

The syntax for the do-while loop looks like this:

do { <your code> } while (<boolean expression>)

Below is the same example we used for the while loop but this time using a do-while loop. In-fact the outputs for both loops are identical.

int counter = 0;
    Console.WriteLine("Iteration: {0}", counter);
} while (counter < 5);

That concludes my explanation of loop structures in C#. I hope you found this article useful. Stay tuned to this blog for more articles in the near future.

Dave runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

1 comment… add one

Leave a Comment