while
loop is the simplest and second most commonly used looping structure in C#. Compared to the for
loop, however, the while
loop is used about as often as metric tools in an American machine shop.The for
loop has this structure:
for(initExpression; condition; incrementExpression)
{
// . . . body of code . . .
}
When the for
loop is encountered, the program first executes the initExpression
expression and then executes the condition
. If the condition
expression is true, the program executes the body of the loop, which is surrounded by the braces immediately following the for
command. When the program reaches the closed brace, control passes to incrementExpression
and then back to condition
, where the next pass through the loop begins. In fact, the definition of a for
loop can be converted into this while
loop:
initExpression;
while(condition)
{
// . . . body of code . . .
incrementExpression;
}
An example in C#
You can better see how thefor
loop works in this example:// Here is one C# expression or another.
a = 1;
// Now loop for awhile.
for(int year = 1; year < duration; year = year + 1)
{
// . . . body of code . . .
}
// The program continues here.
a = 2;
Assume that the program has just executed the a = 1;
expression. Next, the program declares the variable year
and initializes it to 1
. Then the program compares year
to duration
. If year
is less than duration
, the body of code within the braces is executed. After encountering the closed brace, the program jumps back to the top and executes the year = year + 1
clause before sliding back over to the year < duration
comparison.
The year
variable is undefined outside the scope of the for
loop. The loop’s scope includes the loop’s heading as well as its body.
Why do you need another loop?
Why do you need thefor
loop if C# has an equivalent while
loop? The short answer is that you don’t — the for
loop doesn’t bring anything to the table that the while
loop can’t already do.However, the sections of the for
loop exist for convenience — and to clearly establish the three parts that every loop should have: the setup, exit criteria, and increment. Not only is this arrangement easier to read, it’s also easier to get right. (Remember that the most common mistakes in a while
loop are forgetting to increment the counting variable and failing to provide the proper exit criteria.)
The most important reason to understand the for
loop is that it’s the loop everyone uses — and it (along with its cousin, foreach
) is the one you see 90 percent of the time when you’re reading other people’s code.
The for
loop is designed so that the first expression initializes a counting variable and the last section increments it; however, the C# language doesn’t enforce any such rule. You can do anything you want in these two sections; however, you would be ill advised to do anything but initialize and increment the counting variable.
for
loops. The previous for
loop is usually written this way:for(int year = 1; year < nDuration; year<strong>++</strong>)
{
// . . . body of code . . .
}
You almost always see the postincrement operator used in a for
loop instead of the preincrement operator, although the effect in this case is the same. There’s no reason other than habit and the fact that it looks cooler. (The next time you want to break the ice, just haul out your C# listing full of postincrement operators to show how cool you are. It almost never works, but it’s worth a try.)
for
loop has one variation that you may find hard to understand. If the logical condition expression is missing, it’s assumed to be true
. Thus for(;;)
is an infinite loop. You see for(;;)
used as an infinite loop more often than while(true)
.