Mostly Coherent Ramblings of a Silicon Forest Software Developer

C# .Net Tutorials - How to Learn C# Part 6 - Iteration Statements

Author: Jeremy Morgan

Feb 1st, 2013

In part 6 of my learning C# tutorial series we’re going to talk about Iteration Statements in C#. In part 5 we went over C# selection statements, which control program flow with true and false values using statements that execute a statement once based on a criteria. This is great for statements that need to execute in a sequence, but Iteration statements simplify statements that need to be executed in repetition.

usingSystem;namespacetutorials{publicclassTestDoWhile{publicstaticvoidMain(){Console.WriteLine("Count is 0");Console.WriteLine("Count is 1");Console.WriteLine("Count is 2");Console.WriteLine("Count is 3");Console.WriteLine("Count is 4");Console.WriteLine("Count is 5");Console.WriteLine("Count is 6");Console.WriteLine("Count is 7");Console.WriteLine("Count is 8");Console.WriteLine("Count is 9");}}}

You might be wondering why we aren’t counting from 1 to 10. That’s because computers start counting at zero, not one and you have to start getting used to that idea if you’re a beginner. There are ten elements here, but since we started at zero the last digit is 9.

While it’s pretty easy to understand what’s going on here you can tell right away there is a need for optimization. Part of being a “lazy programmer” is not wanting to repeat yourself. This adds time and increases the amount of mistakes.

This code produces the same output yet it’s much less to write and easier to understand. The expression “do” executes and outputs a number (represented by i) then increments that number by adding 1 to it. This do statement would run forever if we let it, so the 2nd part of it is the while statement that evaluates the expression “i < limit”. While checks to see if i is smaller than the limit we specified, and if it is then it returns to execute do again.

Next I’ll explain the Iteration Statements available in C#, and how they work.

Note: Like my other tutorials I will be eliminating the using statements and namespace / class declarations because you shouldn’t repeat yourself in tutorials either ;)

While statement

The while statement is frequently used in code due to it’s simplicity and speed. In the example above I used a combination of do and while to create a do-while loop, but while can operate on it’s own. Let’s do the count to 10 with just a while loop and see how it works:

When you run this, you’ll notice nothing happens. This is because (i) is not less than (limit) so the loop never runs. This is ideal for most situations, but you may want your loop to run at least once, which brings us to the next loop.

Do statement (with while)

I used the do statement in the first example in combination with the while statment. Do executes a statement or block of statements until a specified expression evaluates to FALSE. You can use do without braces if it’s only a single statement:

1

doConsole.WriteLine("Count: "+i);while(i<limit);

but if you need to execute more than one statement you’ll need to enclose them in braces:

12345

do{Console.WriteLine("Count: "+i);i++;}while(i<limit);

The mechanics of this should be fairly obvious: in plain English we want to:

write a value (i) to the console

increment the value

repeat while that value is less than another (limit)

This is pretty simple, but take a note of the order of operations with this loop. The thing to remember about a do-while loop such as the one above is this:

A do-while loop will always execute then evaluate an expression to see if it needs to continue looping. The statement will always be executed at least once, while a while loop will only execute if a condition is evaluated as true. This is an important distinction.

This is because you put in a condition that evaluated whether i was 7 and if so, terminate the loop. This can come in pretty handy. You can also use goto, return and flow to break out of this loop as well. There are times where you may want to check for something in each iteration and if it happens, break out of it to correct it or stop the process.

For Statement

The for loop is a little more tricky to understand at first, but it performs the same function as a while loop. The for loop isn’t faster or any more powerful, but it is easier to construct and read so it’s mostly a programmer advantage (very important).

Here’s an example of a for loop that counts to 10:

1234567

publicstaticvoidMain(){for(inti=0;i<10;i++){Console.WriteLine(i);}}

When you compile and run this it should look pretty familiar to you. The output is the same, but as you can see constructing the loop is easier and there is less code to look at. Let’s explain how it works in plain English:

test to see if our initial value (i) is less than ten

if so, increment it and execute next expression

write out the value (i)

This is my preferred method of looping most of the time because it’s so easy to set up. Here is how the function works:

I hope this has helped clear up how the for loop works. This is a really quick way to iterate through a finite number fairly quick.

Foreach statement

The foreach statement is a little different than for in that it repeats a group of statements for each element in an array, object or collection. It is used for reading those elements in the sequence defined by the foreach statement.

As you can see in this example it’s a little different because we’re creating an array on line 3 that we iterate though. Since this is an integer array we create the int(i) to represent the element of the array. Then we execute everything in the brackets each time a new element in the array is found. While there are new elements in i we will display them, then as soon as there are no more we exit from the loop.

But this doesn’t just work on integer arrays, it works on all types of arrays and objects where you use an iterator. Here’s an example with strings:

Notice how the variable (s) is pulled from the array and displayed until there are no more in the array. This is a great way to iterate through arrays, lists, collections and objects.

Note that you should generally only use this for read only operations. If you start messing with items of an array as it’s being traversed you get some pretty unexpected results.

The foreach statement is great for working with data objects, and is extremely powerful and easy to use.

Conclusion

In this tutorial we explored four kinds of iterations in C#:

while

do

for

foreach

These are what I would consider very commonly used core parts of C# development, but it’s far from the only way to iterate data in C#. In many ways LINQ is taking over this area pretty well, and I’ll cover that in future issues. But learning these statements well will help you on your way to becoming a better C# Programmer.