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
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.
14
-
15
-
## Example
13
+
The `for` statement executes a statement or a block of statements while a specified boolean expression evaluates to `true`.
16
14
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](/dotnet/csharp/language-reference/language-specification/statements#the-for-statement) in the previous example performs the following actions:
22
-
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.
24
-
25
-
2. To evaluate the condition (`i <= 5`), the value of `i` is compared to 5.
26
-
27
-
- If `i` is less than or equal to 5, the condition evaluates to `true`, and the following actions occur.
28
-
29
-
1. The `Console.WriteLine` statement in the body of the loop displays the value of `i`.
30
-
31
-
2. The value of `i` is incremented by 1.
32
-
33
-
3. The loop returns to the start of step 2 to evaluate the condition again.
15
+
At any point within the `for` statement block, you can break out of the loop by using the [break](break.md) statement, or step to the next iteration in the loop by using the [continue](continue.md) statement. You also can exit a `for` loop by the [goto](goto.md), [return](return.md), or [throw](throw.md) statements.
34
16
35
-
- If `i` is greater than 5, the condition evaluates to `false`, and you exit the loop.
36
-
37
-
Note that, if the initial value of `i` is greater than 5, the body of the loop doesn't run even once.
17
+
## Structure of the `for` statement
38
18
39
-
## Sections of a for statement
40
-
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.
19
+
The `for` statement defines *initializer*, *condition*, and *iterator* sections:
42
20
43
-
```csharp
21
+
```csharp
44
22
for (initializer; condition; iterator)
45
23
body
46
-
```
47
-
48
-
The sections serve the following purposes:
49
-
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.
51
-
52
-
- The declaration and initialization of a local loop variable, as the first example shows (`int i = 1`). The variable is local to the loop and can't be accessed from outside the loop.
53
-
54
-
- Zero or more statement expressons from the following list, separated by commas.
- The condition section contains a boolean expression that’s evaluated to determine whether the loop should exit or should run again.
69
-
70
-
- The iterator section defines what happens after each iteration of the body of the loop. The iterator section contains zero or more of the following statement expressions, separated by commas:
- The body of the loop consists of a statement, an empty statement, or a block of statements, which you create by enclosing zero or more statements in braces.
85
-
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.
87
-
88
-
The first example in this topic shows the most typical kind of `for` loop, which makes the following choices for the sections:
89
-
90
-
- The initializer declares and initializes a local loop variable, `i`, that maintains a count of the iterations of the loop.
91
-
92
-
- The condition checks the value of the loop variable against a known final value, 5.
93
-
94
-
- The iterator section uses a postfix increment statement, `i++`, to tally each iteration of the loop.
24
+
```
95
25
96
-
## More examples
97
-
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.
26
+
All three sections are optional. The body of the loop is either a statement or a block of statements.
27
+
28
+
The following example shows the `for` statement with all of the sections defined:
The *condition* sectionintheexampleabovedeterminesiftheloopterminatesbasedonthevalueofthelocalloopvariable:
63
+
64
+
```csharp
65
+
i<5
66
+
```
67
+
68
+
### The *iterator* section
69
+
70
+
The *iterator* section defines what happens after each iteration of the body of the loop. The *iterator* section contains zero or more of the following statement expressions, separated by commas:
- prefix or postfix [increment](../operators/increment-operator.md) expression, such as `++i` or `i++`
77
+
78
+
- prefix or postfix [decrement](../operators/decrement-operator.md) expression, such as `--i` or `i--`
79
+
80
+
- creation of an object by using [new](new-operator.md) keyword
81
+
82
+
-[await](await.md) expression
83
+
84
+
The *iterator* section in the example above increments the local loop variable:
85
+
86
+
```csharp
87
+
i++
88
+
```
89
+
90
+
## Examples
91
+
92
+
The following example illustrates several less common usages of the `for` statement sections: assigning a value to an external loop variable in the *initializer* section, invoking a method in both the *initializer* and the *iterator* sections, and changing the values of two variables in the *iterator* section. Select **Run** to run the example code. After that you can modify the code and run it again.
You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a [jump statement](../../../csharp/language-reference/keywords/jump-statements.md) is encountered.
11
+
You can create loops by using the iteration statements. Iteration statements cause embedded statements to be executed a number of times, subject to the loop-termination criteria. These statements are executed in order, except when a [jump statement](jump-statements.md) is encountered.
12
12
13
13
The following keywords are used in iteration statements:
14
14
@@ -21,6 +21,7 @@ The following keywords are used in iteration statements:
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/indexers/indexers-in-interfaces.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,23 +29,23 @@ Indexers can be declared on an [interface](../../../csharp/language-reference/ke
29
29
In the preceding example, you could use the explicit interface member implementation by using the fully qualified name of the interface member. For example:
30
30
31
31
```
32
-
public string ISomeInterface.this[int index]
32
+
string ISomeInterface.this[int index]
33
33
{
34
34
}
35
35
```
36
36
37
37
However, the fully qualified name is only needed to avoid ambiguity when the class is implementing more than one interface with the same indexer signature. For example, if an `Employee` class is implementing two interfaces, `ICitizen` and `IEmployee`, and both interfaces have the same indexer signature, the explicit interface member implementation is necessary. That is, the following indexer declaration:
38
38
39
39
```
40
-
public string IEmployee.this[int index]
40
+
string IEmployee.this[int index]
41
41
{
42
42
}
43
43
```
44
44
45
45
implements the indexer on the `IEmployee` interface, while the following declaration:
0 commit comments