繁体   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