0

A loop in Eiffel follows this format:

from
    Init
invariant
    Invariant
until
    Exit
variant
    Variant
loop
    Body
end

How would you translate the above Eiffel pseudo-code to a language that does not support loop invariants/variants? Let's assume that such target language has an assert instruction to check the invariant/variant.

Eleno
  • 2,864
  • 3
  • 33
  • 39

1 Answers1

2

It would look like that:

Init
last := infinity
loop
    assert (Invariant)
    next := Variant
    assert (0 <= next and next < last)
    last := next
    if Exit then
        break
    end
    Body
end
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35