Idiom for simple C for i = 0; i < n; i++

i’ve noticed some sample code realizing this sort of simple C for loop using a zig while construct…

i’ve also discovered that for (0..n) |i| { ... } works splendidly in zig…

what are the advantages of one over the other in terms for generality and/or performance ???

The new for loops syntax was just added last year, which might explain while some sample code still uses the while loop.
Also the for loop as one big weakness: It only works with usize. So if you want to e.g loop over i32s you need to use a while loop.

2 Likes

It also does not have support for changing the sequence’s direction or increment.

1 Like

In my head I divide between math and memory. Whenever I want to iterate over memory I use the for-syntax and whenever I use it to compute something or want to do something a number of times I use the while syntax.

2 Likes