abs icon indicating copy to clipboard operation
abs copied to clipboard

A difference between while and for

Open s5unty opened this issue 3 years ago • 1 comments
trafficstars

#!/usr/bin/env abs

f foo() {
    for t=2; t>0; t=t-1 {
        if arg(2) != "ok" {
            sleep(100)
            continue
        }
        return [0, "ok"]
    }
    return [1, "ng"]
}

f bar() {
    t = 2
    while t > 0 {
        t = t - 1
        if arg(2) != "ok" {
            sleep(100)
            continue
        }
        return [0, "ok"]
    }
    return [1, "ng"]
}

a, b = foo()
echo("foo(%s): %s", a, b)

x, y = bar()
echo("bar(%s): %s", x, y)
% ./bug.abs ok
foo(0): ok
bar(1): ng

% ./bug.abs ng
foo(1): ng
ERROR: identifier not found: x
	[33:21]	echo("bar(%s): %s", x, y)

s5unty avatar Dec 18 '21 15:12 s5unty

continue does not have a special meaning for while loops that it has for for loops, therefore, in bar continue simply terminates the function.

AndrewSav avatar Dec 31 '22 10:12 AndrewSav