We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed last year.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
鲁比已经到了。
Ruby has until.
VBA!
Do-Until-Loop
Do-Loop-Until
虽然我认为这里很多人会怀疑它是否是一种真正的语言,但是,BASIC 是 Microsoft 的起步方式(我知道,对许多人来说,这是相当无力的论据)...
VBA!
Do-Until-Loop
Do-Loop-Until
Although I think quite a number of people here would doubt if it is a real language at all, but well, BASIC is how Microsoft started (quite weak argument for many, I know)...
在 VB.Net 中是可能的
在 BS2000(Fujitsu/Siemens 操作系统)上的 SDF-P 中也是可能的
也可以是使用定义
until
的宏的 C 或 C++示例(定义):
示例(使用):
It is possible in VB.Net
It is also possible in SDF-P on BS2000 (Fujitsu/Siemens Operating System)
Is is also possible is C or C++ using a macro that define
until
Example (definition):
Example (utilisation):
在VB中我们可以找到这样的东西:
In VB we can find something like:
Eiffel为您提供了一个until循环。
还有一个“跨”循环。两者都非常强大且富有表现力。
该循环的设计还有更多功能。它的语法还有两个部分将帮助我们解决两个重要的“正确性”问题。
无限循环保护
让我们通过添加循环变体来稍微修改一下循环代码。
循环变量(本质上)是一个倒计时变量,但不仅仅是任何旧变量。通过使用variant关键字,我们告诉编译器注意v。具体来说,编译器将生成在两个条件下监视 v 变量的代码:
v 是否随着循环的每次迭代而减少(我们是否在倒计时) 。如果(事实上)不倒计时,尝试使用倒计时变量是没有好处的,对吗?如果循环变体没有倒计时(减少任意数量),那么我们会抛出异常。
v 是否曾经达到小于零的条件?如果是这样,那么我们会抛出异常。
这两者通过编译器和变量变量一起工作,以检测迭代循环何时以及是否迭代失败或迭代次数过多。
在上面的示例中,我们的代码正在向我们传达一个故事,它希望迭代 0 到 1_000 次,但不会更多。如果更多,那么我们停止循环,这让我们想知道:我们是否真的有迭代超过 1_000 次的情况,或者是否有什么问题导致我们的条件未能变为True ?
循环不变
现在我们知道了循环变体是什么,我们需要了解循环不变是什么。
不变量是一组一个或多个布尔条件,在每次循环迭代后必须保持“True”。我们为什么想要这些?
假设您有 1_000_000 次迭代,其中一次失败。您没有时间遍历每个迭代,检查它是否可以。因此,您创建一组一个或多个条件,并在每次迭代完成时进行测试。如果一个或所有条件失败,那么您就可以准确地知道哪个迭代(及其确定性状态)导致了问题!
循环不变式可能看起来像这样:
在上面的示例中,y 落后 x 1。我们期望每次迭代后,y 将始终为 x - 1。因此,我们创建一个 循环不变性使用invariant关键字来声明我们的布尔断言。如果 y 不等于 x - 1,循环将立即抛出异常,并让我们准确地知道哪次迭代未能保持断言 True。
结论
我们的循环现在非常紧密和安全——可以很好地防止失败(bug、错误)。
Eiffel offers you an until loop.
There is also an "across" loop as well. Both are very powerful and expressive.
The design of this loop has more to offer. There are two more parts to its grammar that will help us resolve two important "correctness" problems.
Endless Loop Protection
Let's modify our loop code a little by adding a loop variant.
The loop variant is (essentially) a count-down variable, but not just any old variable. By using the variant keyword, we are telling the compiler to pay attention to v. Specifically, the compiler is going to generate code that watchdogs the v variable for two conditions:
Does v decrease with each iteration of the loop (are we counting down). It does no good to try and use a count-down variable if it is (in fact) not counting down, right? If the loop variant is not counting down (decreasing by any amount), then we throw an exception.
Does v ever reach a condition of less than zero? If so, then we throw an exception.
Both of these work together through the compiler and variant variable to detect when and if our iterating loop fails to iterate or iterates too many times.
In the example above, our code is communicating to us a story that it expects to iterate zero to 1_000 times, but not more. If it is more, then we stop the loop, which leaves us to wonder: Do we really have cases were we iterate more than 1_000 times, or is there something wrong that our condition is failing to become True?
Loop Invariant
Now that we know what a loop variant is, we need to understand what a loop invariant is.
The invariant is a set of one or more Boolean conditions that must hold True after each iteration through the loop. Why do we want these?
Imagine you have 1_000_000 iterations and one of them fails. You don't have time to walk through each iteration, examining it to see it is okay or not. So, you create a set of one or more conditions that are tested upon completion of each iteration. If the one or all of the conditions fail, then you know precisely which iteration (and its deterministic state) is causing the problem!
The loop invariant might look something like:
In the example above, y is trailing x by 1. We expect that after each iteration, y will always be x - 1. So, we create a loop invariant using the invariant keyword that states our Boolean assertion. If y fails to be x - 1, the loop will immediately throw an exception and let us know precisely which iteration has failed to keep the assertion True.
CONCLUSION
Our loop is now quite tight and secure—well guarded against failure (bugs, errors).