共用方式為


循環:同時...do Expression

表達式 while...do 是用來執行反覆執行(迴圈),而指定的測試條件為 true。

語法

while test-expression do
    body-expression

備註

會評估test-expression;true如果是 ,則會執行body-expression,並再次評估測試表達式。 body-expression 必須有 類型 unit。 如果測試表達式為 false,則反覆項目會結束。

下列範例說明表達式的使用 while...do 方式。

open System

let lookForValue value maxValue =
  let mutable continueLooping = true
  let randomNumberGenerator = new Random()
  while continueLooping do
    // Generate a random number between 1 and maxValue.
    let rand = randomNumberGenerator.Next(maxValue)
    printf "%d " rand
    if rand = value then
       printfn "\nFound a %d!" value
       continueLooping <- false

lookForValue 10 20

上一個程式代碼的輸出是介於 1 到 20 之間的隨機數數據流,最後一個是 10。

13 19 8 18 16 2 10
Found a 10!

備註

您可以在時序表示式和其他計算運算式中使用 while...do ,在此情況下會使用自定義版本的 while...do 表達式。 如需詳細資訊,請參閱 時序異步運算式工作運算式計算運算式

另請參閱