* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
1.2 KiB
1.2 KiB
title, localeTitle
title | localeTitle |
---|---|
For Loop | 对于循环 |
对于循环
for
循环执行代码块,直到指定的条件为false。虽然for
循环看起来像while
循环 ,但开发人员应该正确使用它们。当迭代次数可变时使用while
循环,否则使用for
循环。 for
循环的常见用途是数组迭代。 1
句法
for ((Initial variable); (condition); (step))
{
(code)
}
C#for循环由三个表达式和一些代码组成。
描述
- 初始变量 :您的起始状态,例如int i = 0;
- 条件 :当此条件为真时,代码将继续运行,例如i <= 5;
- 步骤 :初始变量的增量或减量,例如i ++或i- = 2。
例
int[] array = { 1, 2, 3, 4, 5 };
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Item on index {0} is {1}", i, array[i]);
}
输出:
> Item on index 0 is 1
> Item on index 1 is 2
> Item on index 2 is 3
> Item on index 3 is 4
> Item on index 4 is 5
来源
1 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/for