2009-07-30 114 views
3

在我的「本地」編程語言(RPG)中,我可以編寫一個循環,只需離開循環或強制迭代。它有點像GOTO。強制循環迭代

dow (x < 999); 
    read file; 
    if (%eof); 
    leave; // Leave the loop 
    endif; 
    if (field <> fileField); 
    iter; // Iterate to the next record 
    endif; 
enddo; 

我的問題是如果有一個類似的選項是C#。就我而言,我正在使用foreach循環。

回答

18
continue; // Goto the next iteration 
break; // Exit the loop 
+0

1簡潔明瞭;正是我喜歡在答案中看到的。 – 2009-07-30 15:48:26

+0

這就是爲什麼我將它標記爲答案。它沒有比這更清楚。 – 2009-07-30 16:12:33

1

使用continue關鍵字

for (int i = 1; i <= 10; i++) 
    { 
    if (i < 9) 
     continue; 
    Console.WriteLine(i); 
    } 

的這個輸出是:

9 
10