是否有任何语言具有 do-until 循环?

发布于 2024-11-19 01:01:25 字数 1537 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

油饼 2024-11-26 01:01:25

鲁比已经到了。

i=0
begin
  puts i
  i += 1
end until i==5

Ruby has until.

i=0
begin
  puts i
  i += 1
end until i==5
可爱咩 2024-11-26 01:01:25

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)...

摇划花蜜的午后 2024-11-26 01:01:25

在 VB.Net 中是可能的

bExitFromLoop = False 
Do  
    'Executes the following Statement  
Loop Until bExitFromLoop 

在 BS2000(Fujitsu/Siemens 操作系统)上的 SDF-P 中也是可能的

/ DECLARE-VARIABLE A
/ DECLARE-VARIABLE SWITCH-1(TYPE=*BOOLEAN)
/ SET-VARIABLE A = 5
/ SET-VARIABLE SWITCH-1 = ON
/ REPEAT
/   A = A + 10
/   IF (A > 50)
/      SET-VARIABLE SWITCH-1 = OFF
/   END-IF
/ UNTIL (SWITCH-1 = OFF)
/ SHOW-VARIABLE A
A = 55

也可以是使用定义 until 的宏的 C 或 C++

示例(定义):

#define until(cond) while(!(##cond))

示例(使用):

int i = 0;
do  {
    cout << i << "\n";
    i++;
    } until(i == 5);

It is possible in VB.Net

bExitFromLoop = False 
Do  
    'Executes the following Statement  
Loop Until bExitFromLoop 

It is also possible in SDF-P on BS2000 (Fujitsu/Siemens Operating System)

/ DECLARE-VARIABLE A
/ DECLARE-VARIABLE SWITCH-1(TYPE=*BOOLEAN)
/ SET-VARIABLE A = 5
/ SET-VARIABLE SWITCH-1 = ON
/ REPEAT
/   A = A + 10
/   IF (A > 50)
/      SET-VARIABLE SWITCH-1 = OFF
/   END-IF
/ UNTIL (SWITCH-1 = OFF)
/ SHOW-VARIABLE A
A = 55

Is is also possible is C or C++ using a macro that define until

Example (definition):

#define until(cond) while(!(##cond))

Example (utilisation):

int i = 0;
do  {
    cout << i << "\n";
    i++;
    } until(i == 5);
零度℉ 2024-11-26 01:01:25

在VB中我们可以找到这样的东西:

 Reponse = InputBox("Please Enter Pwd")
  Do Until Reponse = "Bob-pwr148" ...

In VB we can find something like:

 Reponse = InputBox("Please Enter Pwd")
  Do Until Reponse = "Bob-pwr148" ...
有木有妳兜一样 2024-11-26 01:01:25

Eiffel为您提供了一个until循环。

from 
  x := 1 
until 
  x > 100 
loop 
  ... 
end

还有一个“跨”循环。两者都非常强大且富有表现力。

该循环的设计还有更多功能。它的语法还有两个部分将帮助我们解决两个重要的“正确性”问题。

  1. 无限循环保护。
  2. 迭代失败检测。

无限循环保护

让我们通过添加循环变体来稍微修改一下循环代码。

from 
  x := 1
  v := 1_000
until 
  x > 100 
variant
  v
loop 
  ...
  v := v - 1 
end

循环变量(本质上)是一个倒计时变量,但不仅仅是任何旧变量。通过使用variant关键字,我们告诉编译器注意v。具体来说,编译器将生成在两个条件下监视 v 变量的代码:

  1. v 是否随着循环的每次迭代而减少(我们是否在倒计时) 。如果(事实上)倒计时,尝试使用倒计时变量是没有好处的,对吗?如果循环变体没有倒计时(减少任意数量),那么我们会抛出异常。

  2. v 是否曾经达到小于零的条件?如果是这样,那么我们会抛出异常。

这两者通过编译器和变量变量一起工作,以检测迭代循环何时以及是否迭代失败或迭代次数过多。

在上面的示例中,我们的代码正在向我们传达一个故事,它希望迭代 0 到 1_000 次,但不会更多。如果更多,那么我们停止循环,这让我们想知道:我们是否真的有迭代超过 1_000 次的情况,或者是否有什么问题导致我们的条件未能变为True

循环不变

现在我们知道了循环变体是什么,我们需要了解循环不变是什么。

不变量是一组一个或多个布尔条件,在每次循环迭代后必须保持“True”。我们为什么想要这些?

假设您有 1_000_000 次迭代,其中一次失败。您没有时间遍历每个迭代,检查它是否可以。因此,您创建一组一个或多个条件,并在每次迭代完成时进行测试。如果一个或所有条件失败,那么您就可以准确地知道哪个迭代(及其确定性状态)导致了问题!

循环不变式可能看起来像这样:

from 
  x := 1
  y := 0
  v := 1_000
invariant
  y = x - 1
until 
  x > 100 
variant
  v
loop 
  ...
  x := x + 1
  y := y + 1
  v := v - 1 
end

在上面的示例中,y 落后 x 1。我们期望每次迭代后,y 将始终为 x - 1。因此,我们创建一个 循环不变性使用invariant关键字来声明我们的布尔断言。如果 y 不等于 x - 1,循环将立即抛出异常,并让我们准确地知道哪次迭代未能保持断言 True

结论

我们的循环现在非常紧密和安全——可以很好地防止失败(bug、错误)。

Eiffel offers you an until loop.

from 
  x := 1 
until 
  x > 100 
loop 
  ... 
end

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.

  1. Endless loop protection.
  2. Iteration failure detection.

Endless Loop Protection

Let's modify our loop code a little by adding a loop variant.

from 
  x := 1
  v := 1_000
until 
  x > 100 
variant
  v
loop 
  ...
  v := v - 1 
end

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:

  1. 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.

  2. 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:

from 
  x := 1
  y := 0
  v := 1_000
invariant
  y = x - 1
until 
  x > 100 
variant
  v
loop 
  ...
  x := x + 1
  y := y + 1
  v := v - 1 
end

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文