#[solved] How to iterate while (true) 0..(modulus N)

1 messages ยท Page 1 of 1 (latest)

twilit walrus
#
int frameID = 0;
while (true) {
  // do things here
  frameID = (frameID + 1) % frame_count;
}
```Is there a clean way to do this in Zig's syntax,
without having to create an outside variable for the looping ID? ๐Ÿค”

If its not possible without a variable...
what's the best way to achieve it cleanly otherwise?
flat adder
#

could do something like this

while (true) for (0..frame_count) |frame_id| {
    _ = frame_id;
    // do stuff
};

is that what your intention is?

twilit walrus
#

where is the modulus there?

#

the goal is to (forever) repeat 0,1,2, 0,1,2, 0,1,2....

flat adder
#

thats what my code will do

twilit walrus
#

oh, you achieve the modulus by breaking/ending the for loop. smart ๐Ÿง 

flat adder
#

yea

twilit walrus
#

that's better even for C, lol

flat adder
#

could ofc change the formatting if its confusing, i just wrote it like that so you didnt double indent

twilit walrus
#

@flat adder I just realized we missed something big
The true condition is only checked at each for break โšฐ๏ธ ๐Ÿ˜”

flat adder
#

uhh im confused wdym

#

is it not actually a true in your code?

twilit walrus
#

the real code is while (should_not_close()) do_frame();

#

yea, every while iteration checks for a frame/app ending condition

flat adder
#

makes sense

twilit walrus
#

Is the modulus variable unavoidable for this usecase? ๐Ÿค”

flat adder
#

i might do something like this

frame_id += 1;
if (frame_id > frame_count) frame_id = 0;

but i dont see anything particularly too bad about the original version

twilit walrus
#

its not that its bad, I was just thinking that there might be a way to achieve the variable modification at the while loop syntax or something

flat adder
#

you can put it in the continue expression, it accepts a block

#
while (cond) : ({
    frame_id += 1;
    if (frame_id > frame_count) frame_id = 0;
}) {

bit ugly though

twilit walrus
#

less ugly with the modulus, right?

flat adder
#

๐Ÿคทโ€โ™‚๏ธ

twilit walrus
#
  var frameID = 0;
  while (condition) : (frameID = (frameID+1) % frames_Len) {
    // do thing here
  }
```This looks better to me
#

I guess that'll have to be good enough ๐Ÿ™‚

#

ty for the helps, as usual ๐Ÿงก ๐Ÿ™

flat adder
#

np