Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C各种类型的循环使用_C#_Loops - Fatal编程技术网

C# C各种类型的循环使用

C# C各种类型的循环使用,c#,loops,C#,Loops,我对C中的循环有点困惑,什么是各种循环的最佳用例,如for、foreach、while、do-while、List.foreach?取决于用例。例如,如果在数组中只需要奇数索引项,请在每次运行中使用带+2的For循环。ForEach适用于标准回路。但在某些情况下,您不能使用其中一个,例如,在foreach中,您不能从集合中删除项目。在这种情况下,你需要。 并且,当您有一个特定的条件时,您需要一个while循环。当您想要设置这样的计数器迭代时,您使用for循环 for(int i=0;i<3

我对C中的循环有点困惑,什么是各种循环的最佳用例,如for、foreach、while、do-while、List.foreach?

取决于用例。例如,如果在数组中只需要奇数索引项,请在每次运行中使用带+2的For循环。ForEach适用于标准回路。但在某些情况下,您不能使用其中一个,例如,在foreach中,您不能从集合中删除项目。在这种情况下,你需要。
并且,当您有一个特定的条件时,您需要一个while循环。

当您想要设置这样的计数器迭代时,您使用for循环

for(int i=0;i<3;i++)//will loop until it meets the condition i<3
{ //statement here}
如果要在语句之前首先满足条件,则使用while

while(condition)
{
//statement here
}
如果要在条件之前先执行语句,则使用do while

do
{
//statement here
}
while(condition)
do
{
//statement here
}
while(condition)