do (C# 參考)do (C# Reference)

當指定的布林運算式評估為 true 時,do 陳述式會執行某個陳述式或陳述式區塊。The do statement executes a statement or a block of statements while a specified Boolean expression evaluates to true. 運算式是在每次執行迴圈之後評估,因此 do-while 迴圈會執行一次以上。Because that expression is evaluated after each execution of the loop, a do-while loop executes one or more times. 這與 while 迴圈不同,此迴圈會執行零次以上。This differs from the while loop, which executes zero or more times.

您可以使用 break 陳述式在 do 陳述式區塊的任一點中斷迴圈。At any point within the do statement block, you can break out of the loop by using the break statement.

您可以使用 continue 陳述式直接逐步執行 while 運算式評估。You can step directly to the evaluation of the while expression by using the continue statement. 如果運算式評估為 true,即繼續執行迴圈中的第一個陳述式。If the expression evaluates to true, execution continues at the first statement in the loop. 否則會在迴圈之後的第一個陳述式繼續執行。Otherwise, execution continues at the first statement after the loop.

您也可以使用 do-while gotoreturnthrow 語句來結束迴圈。You can also exit a do-while loop by the goto, return, or throw statements.


下列範例會示範 do 陳述式的用法。The following example shows the usage of the do statement. 選取 [執行]**** 執行範例程式碼。Select Run to run the example code. 之後,您可以修改程式碼,然後再次執行它。After that you can modify the code and run it again.

int n = 0;
} while (n < 5);

C# 語言規格C# language specification

如需詳細資訊,請參閱 C# 語言規格do 陳述式一節。For more information, see The do statement section of the C# language specification.

另請參閱See also