Skip to content

Commit ceed567

Browse files
gewarrenBillWagner
authored andcommitted
Add direct links to for-statement section of C# language spec (#5405)
Added three direct links to the for-statement section of the C# language spec. Also added more section headings as I found it a bit difficult to parse the topic to find the info on the parts of a for loop.
1 parent 7d8c808 commit ceed567

File tree

1 file changed

+35
-26
lines changed
  • docs/csharp/language-reference/keywords

1 file changed

+35
-26
lines changed

docs/csharp/language-reference/keywords/for.md

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ helpviewer_keywords:
88
- "for keyword [C#]"
99
ms.assetid: 34041a40-2c87-467a-9ffb-a0417d8f67a8
1010
---
11-
# for (C# Reference)
12-
By using a `for` loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to `false`. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
11+
# for (C# reference)
12+
13+
By using a `for` loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to `false`. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.
1314

14-
## Example
15-
In the following example, the value of `i` is written to the console and incremented by 1 during each iteration of the loop.
15+
## Example
16+
17+
In the following example, the value of `i` is written to the console and incremented by 1 during each iteration of the loop:
1618

17-
[!code-csharp[csrefKeywordsIteration#2](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_1.cs)]
19+
[!code-csharp[csrefKeywordsIteration#2](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_1.cs)]
1820

19-
The `for` statement in the previous example performs the following actions.
21+
The [for statement](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement) in the previous example performs the following actions:
2022

21-
1. First, the initial value of variable `i` is established. This step happens only once, regardless of how many times the loop repeats. You can think of this initialization as happening outside the looping process.
23+
1. First, the initial value of variable `i` is established. This step happens only once, regardless of how many times the loop repeats. You can think of this initialization as happening outside the looping process.
2224

23-
2. To evaluate the condition (`i <= 5`), the value of `i` is compared to 5.
25+
2. To evaluate the condition (`i <= 5`), the value of `i` is compared to 5.
2426

2527
- If `i` is less than or equal to 5, the condition evaluates to `true`, and the following actions occur.
2628

@@ -32,16 +34,18 @@ By using a `for` loop, you can run a statement or a block of statements repeated
3234

3335
- If `i` is greater than 5, the condition evaluates to `false`, and you exit the loop.
3436

35-
Note that, if the initial value of `i` is greater than 5, the body of the loop doesn't run even once.
37+
Note that, if the initial value of `i` is greater than 5, the body of the loop doesn't run even once.
38+
39+
## Sections of a for statement
3640

37-
Every `for` statement defines initializer, condition, and iterator sections. These sections usually determine how many times the loop iterates.
41+
Every [for statement](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement) defines *initializer*, *condition*, and *iterator* sections. These sections usually determine how many times the loop iterates.
3842

3943
```csharp
4044
for (initializer; condition; iterator)
4145
body
4246
```
4347

44-
The sections serve the following purposes.
48+
The sections serve the following purposes:
4549

4650
- The initializer section sets the initial conditions. The statements in this section run only once, before you enter the loop. The section can contain only one of the following two options.
4751

@@ -81,29 +85,34 @@ for (initializer; condition; iterator)
8185

8286
You can break out of a `for` loop by using the [break](../../../csharp/language-reference/keywords/break.md) keyword, or you can step to the next iteration by using the [continue](../../../csharp/language-reference/keywords/continue.md) keyword. You also can exit any loop by using a [goto](../../../csharp/language-reference/keywords/goto.md), [return](../../../csharp/language-reference/keywords/return.md), or [throw](../../../csharp/language-reference/keywords/throw.md) statement.
8387

84-
The first example in this topic shows the most typical kind of `for` loop, which makes the following choices for the sections.
88+
The first example in this topic shows the most typical kind of `for` loop, which makes the following choices for the sections:
8589

8690
- The initializer declares and initializes a local loop variable, `i`, that maintains a count of the iterations of the loop.
8791

8892
- The condition checks the value of the loop variable against a known final value, 5.
8993

90-
- The iterator section uses a postfix increment statement, `i++`, to tally each iteration of the loop.
94+
- The iterator section uses a postfix increment statement, `i++`, to tally each iteration of the loop.
95+
96+
## More examples
9197

92-
The following example illustrates several less common choices: assigning a value to an external loop variable in the initializer section, invoking the `Console.WriteLine` method in both the initializer and the iterator sections, and changing the values of two variables in the iterator section.
98+
The following example illustrates several less common choices: assigning a value to an external loop variable in the initializer section, invoking the `Console.WriteLine` method in both the initializer and the iterator sections, and changing the values of two variables in the iterator section.
9399

94-
[!code-csharp[csrefKeywordsIteration#8](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_2.cs)]
100+
[!code-csharp[csrefKeywordsIteration#8](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_2.cs)]
95101

96-
All of the expressions that define a `for` statement are optional. For example, the following statement creates an infinite loop.
102+
All of the expressions that define a `for` statement are optional. For example, the following statement creates an infinite loop:
97103

98-
[!code-csharp[csrefKeywordsIteration#3](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_3.cs)]
104+
[!code-csharp[csrefKeywordsIteration#3](../../../csharp/language-reference/keywords/codesnippet/CSharp/for_3.cs)]
99105

100-
## C# Language Specification
101-
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
106+
## C# language specification
107+
108+
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
102109

103-
## See Also
104-
[C# Reference](../../../csharp/language-reference/index.md)
105-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
106-
[C# Keywords](../../../csharp/language-reference/keywords/index.md)
107-
[foreach, in](../../../csharp/language-reference/keywords/foreach-in.md)
108-
[for Statement (C++)](/cpp/cpp/for-statement-cpp)
109-
[Iteration Statements](../../../csharp/language-reference/keywords/iteration-statements.md)
110+
## See also
111+
112+
[The for statement (C# language specification)](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement)
113+
[C# Reference](../../../csharp/language-reference/index.md)
114+
[C# Programming Guide](../../../csharp/programming-guide/index.md)
115+
[C# Keywords](../../../csharp/language-reference/keywords/index.md)
116+
[foreach, in](../../../csharp/language-reference/keywords/foreach-in.md)
117+
[for Statement (C++)](/cpp/cpp/for-statement-cpp)
118+
[Iteration Statements](../../../csharp/language-reference/keywords/iteration-statements.md)

0 commit comments

Comments
 (0)