2015-08-09 52 views

回答

0

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

0

当你想设置一个计数器,您使用循环迭代这样

for(int i=0;i<3;i++)//will loop until it meets the condition i<3 
{ //statement here} 

您使用的foreach如果你要循环,并显示这些变量的集合

string[] name = { "josh", "aj", "beard" }; 

    // ... Loop with the foreach keyword. 
    foreach (string value in name) 
    { 
     Console.WriteLine(name); 
    } 

而为如果您想在声明之前先满足条件,请使用

while(condition) 
{ 
//statement here 
} 

do while如果您想在条件前先做说明,请使用

do 
{ 
//statement here 
} 
while(condition)