Domanda

Im trying to understand how modulus is used to make something happen every Nth cycle in a loop with a float. Ive seen lots of examples and tutorials but none of them makes it clear for me how the different parts of the modulus expression works so that i can make it work the way i want.

In other words i see examples on how to make something happen every third cycle or every second cycle. But i dont understand how i should modify it to make something happen every 12th cycle or whatever nr.

È stato utile?

Soluzione

You do not specify a language so this is in C++

for (int i = 0; i < 100; ++i)
{
    // Do stuff
    if (i % 12 == 0)
    {
        // In addition to the regular actions in this
        // loop, do stuff every 12th iteration of the loop,
        // starting with the first
    }
}

Here I am assuming that when you use the term modulus you are referring to arithmetic modulo N.

If this is not what you actually mean in your question, perhaps you can edit it to provide code for the examples and tutorials that you are referring to.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top