开发者

What does “foo = (foo + 1) % bar” exactly do?

开发者 https://www.devze.com 2022-12-28 19:51 出处:网络
From the Finch audio library: - (void) play { [[sounds objectAtIndex:current] play]; current = (current + 1) % [sounds count]; // this line here...

From the Finch audio library:

- (void) play
{
    [[sounds objectAtIndex:current] play];
    current = (current + 1) % [sounds count]; // this line here...
}

I try to grok it: There is a number of sounds n, and current is increased by 1 on every iteration. As soon as current is bigger th开发者_如何学JAVAan number of sounds n, the modulo returns zero. That way, it starts from the beginning.

Is this correct?


Yes, that's right.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号