Swift-repeat-while-loop

提供:Dev Guides
移動先:案内検索

Swift-do …​ whileループ

ループの上部でループ条件をテストする for および while ループとは異なり、 repeat …​ while ループはループの下部でその状態をチェックします。

*repeat ... while* ループはwhileループに似ていますが、 *repeat ... while* ループは少なくとも1回実行されることが保証されています。

構文

Swift 4の repeat …​ while ループの構文は-

repeat {
   statement(s);
}
while( condition );

条件式はループの最後に現れるため、条件がテストされる前にループ内のステートメントが1回実行されることに注意してください。 条件が真の場合、制御フローは repeat にジャンプして戻り、ループ内のステートメントが再度実行されます。 このプロセスは、指定された条件が偽になるまで繰り返されます。

数値0、文字列 '0’と ""、空のlist()、およびundefは、ブールコンテキストではすべて false であり、他のすべての値は true です。 !*または *not による真の値の否定は、特別な偽の値を返します。

流れ図

繰り返し繰り返しループ

var index = 10

repeat {
   print( "Value of index is \(index)")
   index = index + 1
}
while index < 20

上記のコードが実行されると、次の結果が生成されます-

Value of index is 10
Value of index is 11
Value of index is 12
Value of index is 13
Value of index is 14
Value of index is 15
Value of index is 16
Value of index is 17
Value of index is 18
Value of index is 19