C# for/foreach Loop
A for A loop is a repetitive control structure that allows you to write a loop that executes a specific number of times.
Syntax
In C#, for The syntax of the do...while loop:
for ( init; condition; increment )
{
statement(s);
}
The following is the control flow of a for loop:
-
init is executed first and only once. This step allows you to declare and initialize any loop control variables. You may also write no statement here, as long as a semicolon appears.
-
Next, the conditionis evaluated. If true, the loop body executes. If false, the loop body is not executed and control flow jumps to the statement immediately following the for loop.
-
After executing the for loop body, control flow jumps back to the above increment statement. This statement allows you to update the loop control variable. This statement can be left empty, as long as a semicolon appears after the condition.
-
The condition is evaluated again. If true, the loop executes; this process repeats (loop body, then increment step, then re-evaluate condition). The for loop terminates when the condition becomes false.
Flowchart
Example
Example
using
System
;
namespace
Loops
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
/* for loop execution */
for
(
int
a
=
10
;
a
<
20
;
a
=
a
1
)
{
Console
.
WriteLine
(
"Value of a: {0}"
, a
)
;
}
Console
.
ReadLine
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19
foreach
C# also supports the foreach loop; using foreach you can iterate over an array or a collection object.
C#'s foreach loop can be used to traverse collection types such as arrays, lists, and dictionaries. It is a simplified version of the for loop, making the code more concise and readable.
The following is the syntax of the foreach loop:
foreach (var item in collection)
{
// 循环
}
collection is the collection to be traversed, and item is the current element being traversed.
The following example has three parts:
- Output the elements of an integer array via a foreach loop.
- Output the elements of an integer array via a for loop.
- The foreach loop sets the counter of the array elements.
Example
class
ForEachTest
{
static
void
Main
(
string
[
]
args
)
{
int
[
]
fibarray
=
new
int
[
]
{
0
,
1
,
1
,
2
,
3
,
5
,
8
,
13
}
;
foreach
(
int
element
in
fibarray
)
{
System
.
Console
.
WriteLine
(
element
)
;
}
System
.
Console
.
WriteLine
(
)
;
// Similar to a foreach loop
for
(
int
i
=
0
;
i
<
fibarray
.
Length
;
i
++
)
{
System
.
Console
.
WriteLine
(
fibarray
[
i
]
)
;
}
System
.
Console
.
WriteLine
(
)
;
// Set the counter of elements in the collection
int
count
=
0
;
foreach
(
int
element
in
fibarray
)
{
count
+=
1
;
System
.
Console
.
WriteLine
(
"Element #{0}: {1}"
, count, element
)
;
}
System
.
Console
.
WriteLine
(
"Number of elements in the array: {0}"
, count
)
;
}
}
The output is:
0
1
1
2
3
5
8
13
0
1
1
2
3
5
8
13
Element #1: 0
Element #2: 1
Element #3: 1
Element #4: 2
Element #5: 3
Element #6: 5
Element #7: 8
Element #8: 13
Number of elements in the array: 8
The following example uses foreach to traverse a list:
Example
using
System
;
using
System.Collections.Generic
;
class
Program
{
static
void
Main
(
string
[
]
args
)
{
// Create a list of strings
List
<
string
myStrings
=
new
List
<
string
(
);
// Add some string elements to the list
myStrings
.
Add
(
"Google"
)
;
myStrings
.
Add
(
"Runoob"
)
;
myStrings
.
Add
(
"Taobao"
)
;
// Use a foreach loop to traverse the elements in the list
foreach
(
string
s
in
myStrings
)
{
Console
.
WriteLine
(
s
)
;
}
// Wait for the user to press any key before exiting the program
Console
.
ReadKey
(
)
;
}
}
When the code above is compiled and executed, it produces the following result:
Google
Runoob
Taobao
For more information about foreach, see:Usage of foreach Traversal in C#
C# Loops