Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
27
Добавлен:
25.03.2016
Размер:
14 Мб
Скачать
☆

Chapter 5 Using Compound Assignment and Iteration Statements

91

Writing for Statements

Most while statements have the following general structure:

initialization

while (Boolean expression)

{

statement

update control variable

}

With a for statement, you can 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

You can rephrase the while loop shown earlier that displays the integers from 0 to 9 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. Then, if the Boolean expression evaluates to true, the statement runs. The control variable update occurs, and then the Boolean expression is reevaluated. 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, that the statement in the body of the loop always executes before the update occurs, and that the update occurs before the Boolean expression reevaluates.

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!”);

}

92

Part I Introducing Microsoft Visual C# and Microsoft Visual Studio 2008

 

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, even when they are omitted.

 

If necessary, you can provide multiple initializations and multiple updates in a for loop (you

 

can have only 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--)

 

{

 

...

 

}

 

As a final example, here is the while loop from the preceding exercise recast as a for loop.

 

for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())

 

{

 

source.Text += line + ‘\n’;

 

}

 

Tip It’s considered good style to use braces to explicitly delineate the 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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]