Whiley / WhileyDocs

Various documents relating to the Whiley Programming Language.
7 stars 2 forks source link

continue example is broken #29

Open ePaul opened 8 years ago

ePaul commented 8 years ago

From chapter 5 of the specification (eg_6.whiley), the example for continue:

function sumNonNegative(int[] xs) -> int:
    int i = 0
    int r = 0
    while i < |xs|:
        if xs[i] < 0:
            continue
        r = r + xs[i]
        i = i + 1
    return r

While this example demonstrates the continue statement, it also will cause an endless loop if the array actually contains a negative value (because the i = i + 1 is also skipped).

I'm not sure how this example could be fixed to do what the function name pretends, and still use continue.

DavePearce commented 8 years ago

Hmmm, interesting. Yes, I suppose an example which always terminates would be better!!