开发者

Are there any languages that have a do-until loop? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 18 days ago.

开发者_Go百科 Improve this question

Is there any programming language that has a do-until loop?

Example:

do
{
    <statements>
}
until (<condition>);

which is basically equivalent to:

do
{
    <statements>
}
while (<negated condition>);

NOTE: I'm looking for post-test loops.


Ruby has until.

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


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


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


In VB we can find something like:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜