ios 在 Swift 中执行 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35128007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:24:00  来源:igfitidea点击:

do while loop in Swift

iosswift

提问by Sazzad Hissain Khan

How to write a do-while loop in Swift?

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

回答by Sazzad Hissain Khan

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 的所有正值,此循环将重复。

回答by Lorem Ipsum Dolor

repeat-whileloop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-whileloop does).

repeat-while循环,在考虑循环的条件之前首先执行循环块的单次传递(正是do-while循环的作用)。

Code snippet below is a general form of a repeat-whileloop,

下面的代码片段是repeat-while循环的一般形式,

repeat {
  // your logic
} while [condition]

回答by Dhaval Gevariya

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

回答by RakeshDipuna

swift's repeat-while loop is similar to a do-while loop in other language . 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.

swift 的 repeat-while 循环类似于其他语言中的 do-while 循环。repeat-while 循环是一个交替的 while 循环。它首先通过循环块,然后考虑循环条件并重复循环,直到条件显示为假。

repeat 
{
x--
}while x > 0