حلقات: بينما... هل تعبير (F#)

The while...do تعبير is used إلى perform iterative execution (looping) while a specified حالة الاختبار هو صحيح.

while test-expression do
   body-expression

ملاحظات

The test-expression هو evaluated; if it هو true, the body-expression هو executed و the اختبار تعبير هو evaluated again. The body-expression must have نوع unit. If the اختبار تعبير هو false, the تكرار ends.

The following مثال illustrates the استخدم of the 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

The إخراج of the السابق تعليمات برمجية هو a stream of عشوائية أرقام between 1 و 20, the أخير of which هو 10.

13 19 8 18 16 2 10
Found a 10!

ملاحظة

You can استخدم while...do في تسلسل expressions و غير ذلك computation expressions, في which حالة a مخصص الإصدار of the while...do تعبير هو used. لمزيد من المعلومات، راجع تسلسلات (F#) ، مهام سير العمل غير متزامن (F#) ، و التعبيرات احتساب (F#).

راجع أيضًا:

المرجع

حلقات: for.... في تعبير (F#)

حلقات: for... إلى تعبير (F#)

موارد أخرى

مرجع لغة ب #