0

Need to do somethin for each of every fifth iteration, somehow, this doesn't quite work, can't get the gist of it, what did I do wrong:

for (int i=1; i<1000; i++) 
{
    if (i % 1 == 0) 
    {
        // Do something here for all iterations that end in 1 or 6
        Console.WriteLine("Iteration:" + i.ToString());
    }
    if (i % 2 == 0)
    {
        // Do something here for all iterations that end in 2 or 7
        Console.WriteLine("Iteration:" + i.ToString());
    }
    if (i % 3 == 0)
    {
        // Do something here for all iterations that end in 3 or 8
        Console.WriteLine("Iteration:" + i.ToString());
    }
    if (i % 4 == 0)
    {
        // Do something here for all iterations that end in 4 or 9
        Console.WriteLine("Iteration:" + i.ToString());
    }
    if (i % 5 == 0)
    {
        // Do something here for all iterations that end in 5 or 0
        Console.WriteLine("Iteration:" + i.ToString());
    }
};
jamesnet214
  • 1,044
  • 13
  • 21
MB34
  • 4,210
  • 12
  • 59
  • 110
  • you-d be better using a switch case – Rodrigo Rodrigues May 23 '21 at 23:48
  • 1
    Note that `i % 1` is the remainder when dividing by 1, i.e., always 0. – vonbrand May 23 '21 at 23:56
  • There are a few answers here trying to improve the code - but it contains just a small error (almost a typo) - all `if`-s should be `if (i % 5 == 2)` instead of `if (i % 2 == 0)`. `i % 2 == 0` check if the number is even (0 reminder when dividing by 2). `i % 5 == 2` checks if the number ends with 2 or 7 - reminder is 2 when divided by 5. – Kobi May 23 '21 at 23:56
  • The operand of the remainder operator is the _divisor_, not the expected remainder. See duplicates for more details about how to use it correctly. – Peter Duniho May 23 '21 at 23:57

3 Answers3

3

You only need to compute the remainder once per iteration, like this:

for (int i=1; i<1000; i++) {
    switch (i % 5) {
        case 0: Console.WriteLine("Remainder 0"); break;
        case 1: Console.WriteLine("Remainder 1"); break;
        case 2: Console.WriteLine("Remainder 2"); break;
        case 3: Console.WriteLine("Remainder 3"); break;
        case 4: Console.WriteLine("Remainder 4"); break;
    }
}
Brian Berns
  • 15,499
  • 2
  • 30
  • 40
  • Ah, I hadn't thought of it this way. This is better than my answer, good call. – Matt U May 23 '21 at 23:52
  • Alternative: `switch (i % 10) { case 1: case 6: Console.WriteLine("Action 1/6"); break; ... } }` *Note:* @brianberns solution is shorter, this one might be more readable as it incorporates the text of your comments in the code and humans are used to thinking base 10 ... ;-> – AlWo23 May 24 '21 at 00:13
3

You're so close. You instead want to do case analysis on i % 5.

for (int i = 1; i < 10000; i++) {
   switch (i % 5) {
      case 1:
         <Do whatever you do for last digit of 1 or 6>;
         break;
      case 2:
         <Do whatever you do for last digit of 2 or 7>; 
         break;
...
    }
}
Mark Saving
  • 1,752
  • 7
  • 11
0

If I understand the question correctly, you could always have a separate counter that you reset manually:

var iteration = 0;
for (var i = 0; i < 1000; i++)
{
    iteration++;
    if (i == 1)
    {
        // First iteration
    }
    else if (i == 2)
    {
        // Second iteration
    }
    // And so on

    if (iteration >= 5)
    {
        iteration = 0;
    }
}
Matt U
  • 4,970
  • 9
  • 28