Previous Page
Next Page

Writing for Statements

Most while statements have the following general structure:

initialization 
while (Boolean expression) 
{ 
  statement 
  update control variable 
}

A for statement allows you to write a more formal version of this kind of construct by combining the initialization, the Boolean expression, and the update (the loop's “housekeeping”). You'll find the for statement useful because it is much harder to forget any one of the three parts. Here is the syntax of a for statement:

for (initialization; Boolean expression; update control variable) 
    statement

The while loop shown earlier, that displays the integers from 0 to 9, can be reconstructed as the following for loop:

for (int i = 0; i != 10; i++) 
{ 
    Console.WriteLine(i); 
}

The initialization occurs once at the start of the loop. If the Boolean expression evaluates to true, the statement runs. The control variable update occurs and the Boolean expression is re-evaluated. If the condition is still true, the statement is executed again, the control variable is updated, the Boolean expression is evaluated again, and so on.

Notice that the initialization occurs only once, and that the statement in the body of the loop always executes before the update occurs, and that the update occurs before the Boolean expression evaluates.

You can omit any of the three parts of a for statement. If you omit the Boolean expression, it defaults to true. The following for statement runs forever:

for (int i = 0; ;i++) 
{ 
    Console.WriteLine("somebody stop me!"); 
}

If you omit the initialization and update parts, you have a strangely spelled while loop:

int i = 0; 
for (; i != 10; ) 
{ 
    Console.WriteLine(i); 
    i++; 
}
NOTE
The initialization, Boolean expression, and update control variable parts of a for statement must always be separated by semicolons.

If necessary, you can provide multiple initializations and multiple updates in a for loop (you can only have one Boolean expression). To achieve this, separate the various initializations and updates with commas, as shown in the following example:

for (int i = 0, j = 10; i <= j; i++, j--) 
{ 
    ... 
}
TIP
It's considered good style to use an explicit statement block for the body of if, while, and for statements even when the block contains only one statement. By writing the block, you make it easier to add statements to the block at a later date. Without the block, to add another statement, you'd have to remember to add both the extra statement and the braces, and it's very easy to forget the braces.
Understanding for Statement Scope

You might have noticed that you can declare a variable in the initialization part of a for statement. That variable is scoped to the body of the for statement and disappears when the for statement finishes. This rule has two important consequences. First, you cannot use that variable after the for statement has ended, because it's no longer in scope. Here's an example:

for (int i = 0; i != 10; i++) 
{ 
    ... 
} 
Console.WriteLine(i); // compile time error

Second, you can write two or more for statements next to each other that use the same variable name, because each variable is in a different scope. Here's an example:

for (int i = 0; i != 10; i++) 
{ 
    ... 
} 
 for (int i = 0; i != 20; i += 2) // okay 
{ 
    ... 
}

Previous Page
Next Page