The For Loop


This section discusses the for loop, introduced in the previous code snippet. What you have here is the C# equivalent of this Visual Basic code:

Dim I As Integer For I = 1 To 3    listBox1.Items.Add "Details of the Employee" Next

The idea of the For loop in Visual Basic is that you start off by initializing a variable, called the loop control variable, and each time you go round the loop, you add something to the loop control variable until it exceeds a final value. This is quite useful, but gives you almost no flexibility in how the loop works. Although you can change the value of the increment, or even make the increment negative, by using the Step facility, the loop always works by counting, and the test of whether the loop exits is always whether the variable has reached a preset minimum or maximum value.

In C# the for loop generalizes this concept. The basic idea of the for loop in C# is this: At the beginning of the loop you do something, at each step of the loop you do something else in order to move to the next iteration, and in order to determine when to exit from the loop, you perform some test. The following table provides a comparison between the Visual Basic and C# versions of this loop.

Loop

Visual Basic

C#

At start of loop...

Initialize the loop control variable.

Do something.

To test whether to exit loop...

Check whether the loop control variable has reached a certain value.

Test some condition.

At end of each iteration...

Increment the loop control variable.

Do something.

This might look a bit vague, but it does give you a lot of flexibility! For example, in C#, instead of adding a quantity to the loop control variable at each iteration, you might multiply its value by some number. Or instead of adding on a fixed amount you might add some number that you've read in from a file and which changes with each iteration. The test doesn't have to be a test of the value of the loop control variable. It could be a test of whether you have reached the end of the file. What this adds up to is that, by a suitable choice of the start action, test, and action at the end of each iteration, the for loop can effectively perform the same task as any of the other loops in Visual Basic (For, For Each, Do, and While). Alternatively the loop can work in some manner for which there is no simple equivalent in Visual Basic. The C# for loop really gives you complete freedom to control the loop in whatever manner is appropriate for the task at hand.

Note

It should be noted, however, that C# also does support foreach, do, and while loops.

Now let's look at the syntax. The C# version of the previous for loop looks like this:

 for (int I=0 ; I<3 ; I++)  {  this.listBox1.Items.Add(Employees[I].Name);  this.listBox1.Items.Add(Employees[I].ToString());  this.listBox1.Items.Add("");  }  

As you can see, the for statement itself takes three different items inside its parentheses. These items are separated by semicolons:

  • The first item is the action that is performed right at the start of the loop in order to initialize the loop. In this case you declare and initialize the loop control variable.

  • The next item is the condition that will be evaluated to determine whether the loop should exit.

In this case your condition is that I must be less than 3. The loop continues as long as this condition is true and exits as soon as the condition evaluates to false. The condition will be evaluated at the beginning of each iteration, so that if it turns out to be false right at the start, the statement inside the loop does not get executed at all.

  • In the third item is the statement that is executed at the end of each iteration of the loop. Visual Basic loops always work by incrementing some number.

Even though the syntax looks unfamiliar, once you've familiarized yourself with it, you can use the for loop in very powerful ways. For example, if you want to display all the integer powers of 2 that are less than 4000 in a list box, you can write this:

 for (int I = 2 ; I<4000 ; I*=2) listBox1.Items.Add(I.ToString());  

You can achieve the same result in Visual Basic, but it wouldn't be as easy; for this particular example, you might want to opt for a while loop in Visual Basic.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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