2015-08-09 74 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)