简体   繁体   English

在 Swift 中执行 while 循环

[英]do while loop in Swift

如何在 Swift 中编写 do-while 循环?

Here's the general form of a repeat-while loop for Swift这是 Swift 的 repeat-while 循环的一般形式

repeat {
    statements
} while condition

For example,例如,

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

This loop will repeat for all positive values of n.对于 n 的所有正值,此循环将重复。

repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does). 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 loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does). repeat-while循环,在考虑循环的条件(正是do-while循环所做的)之前首先执行循环块的单次传递。

Code snippet below is a general form of a repeat-while loop,代码段下面是一个的一般形式repeat-while循环,

repeat {
  // your logic
} while [condition]

swift's repeat-while loop is similar to a do-while loop in other language . swift 的 repeat-while 循环类似于其他语言中的 do-while 循环。 The repeat-while loop is a alternate while loop.It first makes a single pass through the loop block ,then considers the loop condition and repeats the loop till the condition shows as false. repeat-while 循环是一个交替的 while 循环。它首先通过循环块,然后考虑循环条件并重复循环,直到条件显示为假。

repeat 
{
x--
}while x > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM