logoalt Hacker News

forgotpwd16 • today at 4:57 PM • 0 replies • view on HN

Funnily, Zig does allow doing it explicitly like that, just need use while rather for:

    var i: usize = 0;
    while (i <= n) : (i += 1) {
        total += a[i];
    }
That said, perhaps Odin, for which simplicity and readability is guiding principle in its design, is more up your alley. In Odin, can be read:

    for i := 0; i <= n; i += 1 {
        total += a[i]
    }
Exactly same style can be used in Go & V (which is/was? quite infamous here in HN). But in all languages, for this example at least, a foreach-like iteration, i.e.

    for x in a {
        total += x
    }
would be preferred.