Looping and Repetition


In addition to conditional logic and branching, looping is one of the most common tasks performed by all applications, regardless of the application type. It is impossible to find a production-quality application written in .NET today that does not include some use of loops.

A loop is a repetition of a code block a certain number of times. The number of times is determined by the type of loop being used. This section will show you the basics of creating and using loops in C#.

Using the for Loop

Each for loop has three sections:

  1. The initializer. This is a block of code that is executed once at the beginning of the for loop. Variables declared in this section are available for the scope and duration of the loop but will not remain outside the loop. This section is typically used to initialize the counter variable or variables.

  2. The exit condition. This section defines a Boolean expression that is used to determine how long the for loop keeps running. As long as the Boolean expression continues to evaluate to true, the loop keeps running. This expression is tested at the beginning of each loop execution.

  3. The iterative code. This section is arbitrary, and can contain any code that you want to execute at the end of each iteration through the loop. This is typically where the loop counter variables are incremented.

The format of the for loop is as follows:

for ( initializer; exit condition; iterative code ) {     ... } 


So, to create a for loop that will execute a block of code five times, you would write the following:

for ( int x = 0; x < 5; x ++)     Console.WriteLine("The value is " + x.ToString() ); 


This will print the following output:

0 1 2 3 4 


Using the while Loop

Whereas the for loop is typically used to provide an indexed (a counter variable) loop, the while loop will continually perform the same action over and over again so long as a given Boolean expression evaluates to true. The format of this loop is as follows:

while ( expression ) { ... } 


If you wanted to perform an action until some flag indicated that you could not perform it any more, your while xloop might look something like the following:

bool canContinue = true; while (canContinue) {     ... perform action ...     canContinue = ... } 


The expression is evaluated at the beginning of each loop execution, and if the evaluation is false, the code within the loop will not be executed. A common mistake when using while loops is neglecting to set the exit condition for the loop. In the preceding example, this is the canContinue variable. If you don't set the exit condition for the while loop, it will run forever (or until you shut the application down forcefully).

Using the do Loop

The do loop is similar to the while loop, except that the Boolean expression is evaluated at the end of the loop execution instead of at the beginning of the loop. The format of the do loop is as follows:

do {     ... } while ( expression ); 


The use of the do loop always guarantees that the code inside the loop will execute at least once. All the other loop types have the ability to execute 0 times if the entry condition fails. Here is a sample do loop:

int x = 1; do {     Console.WriteLine(x);     x++; } while ( x < 10 ); 




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net