Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Help using the Mod Function for looping in C#

Tags:

c#

loops

modulo

for (int i = 0; i < 100; i = (i%10))
{
    age = (age +10);
    int_year = (int_year + 10);

So I am trying to use the Mod function for looping in C#, the requirement is that it the int_year and age loop 100 times but only every 10th increment is kept. Thanks in advance :)

like image 933
user3063971 Avatar asked Aug 03 '26 21:08

user3063971


2 Answers

Why not just mod 10 within the loop to determine if this is the 10th iteration:

for (int i = 0; i < 100; i++)
{
    if (i % 10 == 0)
    {
         // Every 10th iteration
    }
}
like image 150
Haney Avatar answered Aug 06 '26 12:08

Haney


You can use step equal to 10:

for (int i = 0; i < 100; i += 10)
{
      // Every 10th iteration
}

I think it's more efficient than using mod.

like image 31
Tony Avatar answered Aug 06 '26 10:08

Tony