You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/for.md
+35-26Lines changed: 35 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,19 +8,21 @@ helpviewer_keywords:
8
8
- "for keyword [C#]"
9
9
ms.assetid: 34041a40-2c87-467a-9ffb-a0417d8f67a8
10
10
---
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.
13
14
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:
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:
20
22
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.
22
24
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.
24
26
25
27
- If `i` is less than or equal to 5, the condition evaluates to `true`, and the following actions occur.
26
28
@@ -32,16 +34,18 @@ By using a `for` loop, you can run a statement or a block of statements repeated
32
34
33
35
- If `i` is greater than 5, the condition evaluates to `false`, and you exit the loop.
34
36
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
36
40
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.
38
42
39
43
```csharp
40
44
for (initializer; condition; iterator)
41
45
body
42
46
```
43
47
44
-
The sections serve the following purposes.
48
+
The sections serve the following purposes:
45
49
46
50
- 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.
47
51
@@ -81,29 +85,34 @@ for (initializer; condition; iterator)
81
85
82
86
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.
83
87
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:
85
89
86
90
- The initializer declares and initializes a local loop variable, `i`, that maintains a count of the iterations of the loop.
87
91
88
92
- The condition checks the value of the loop variable against a known final value, 5.
89
93
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
91
97
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.
0 commit comments