簡體   English   中英

在 Swift 中執行 while 循環

[英]do while loop in Swift

如何在 Swift 中編寫 do-while 循環?

這是 Swift 的 repeat-while 循環的一般形式

repeat {
    statements
} while condition

例如,

repeat {
    //take input from standard IO into variable n
} while n >= 0;

對於 n 的所有正值,此循環將重復。

repeat-while 循環,在考慮循環的條件(正是 do-while 循環所做的)之前首先執行循環塊的單次傳遞。

var i = Int()
repeat {
//below line was fixed to say print("\(i) * \(i) = \(i * i)")
   print("\(i) * \(i) = \(i * i)")
    i += 1

} while i <= 10

repeat-while循環,在考慮循環的條件(正是do-while循環所做的)之前首先執行循環塊的單次傳遞。

代碼段下面是一個的一般形式repeat-while循環,

repeat {
  // your logic
} while [condition]

swift 的 repeat-while 循環類似於其他語言中的 do-while 循環。 repeat-while 循環是一個交替的 while 循環。它首先通過循環塊,然后考慮循環條件並重復循環,直到條件顯示為假。

repeat 
{
x--
}while x > 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM