As mentioned in Chapter 3, the foreach statement can be used to iterate through collections or arrays of objects. Other iterator statements include for, while, and do … while.
The simplest loop or iterator statement is the for statement.
for(value; condition expression; incrementor/decrementor)
{
Statements
}
The for loop has three parts:
A value, which assigns the starting count for the loop
Followed by a conditional expression, which determines the end count for the loop
And finally, an incrementor/decrementor, which increments or decrements the count of the first value
You may also nest for loops if you desire.
A new addition to C# is the foreach statement, which is another type of iterator.
foreach(type Identifier in collection/array)
{
Statements
}
In a foreach statement, the first operand is always the type that is being added to the collection or array defined in the second operand.
The following example demonstrates the use of for and foreach loops. (Also see Chapter 4 for more examples of using foreach with arrays and collections.)
using System; namespace Client.Chapter_4___Program_Control { class Fors { static void Main(string[] args) { //a is set to zero and incremented with each iteration until //a is less than 10 for (int a = 0; a < 10; a++) { Console.WriteLine(a); } } } } using System; using System.Collections; namespace Client.Chapter_4___Program_Control { class ForEaches { static void Main(string[] args) { ArrayList a = new ArrayList(10); //Each member of the ArrayList will be iterated foreach (int x in a) { Console.WriteLine(x); } } } }
The while loop executes statements within the code block of the while loop only if the expression within the parentheses evaluates to true. If the statement within the code block evaluates to false, then the block of while code is ignored, and control passes to the first statement following the while block.
| Caution |
When using a while loop, make sure that the condition of the while loop is such that it will eventually be false and exit, or else you will have a spinning thread and your application will become unresponsive. You should also make sure that you increment or decrement the iterator if you are using one inside the loop. |
Another version of the while loop is the do … while loop. The big difference between the while loop and the do … while loop is that the do … while loop code block is guaranteed to execute at least once.
In the following example, the while loop will print 1 through 10 using Console.WriteLine.
using System; namespace Client.Chapter_4___Program_Control { class Whiles { static void Main(string[] args) { int a = 0; //While the value of a is greater than 10, //the inner code will be executed while(a > 10) { a++; Console.WriteLine(a); } } } } using System; namespace Client.Chapter_4___Program_Control { class DoWhile { static void Main(string[] args) { int a = 0; //The do loop is a normal while loop with one exception: //the code in the block is guaranteed to be executed //at least once do { a++; Console.WriteLine(a); } while (a < 10); } } }