Home > Software engineering >  how to skip every other iteration
how to skip every other iteration

Time:10-20

I want to output a range of numbers, and break them out into separate rows:

1: ABC-1 - ABC-100
2: ABC-101 - ABC-200
3: ABC-201 - ABC-300
4: ABC-301 - ABC-400
5: ABC-401 - ABC-500
6: ABC-501 - ABC-600
7: ABC-601 - ABC-700
8: ABC-701 - ABC-800
9: ABC-801 - ABC-900
10: ABC-901 - ABC-1000

I did that with this:

using System;

class Program
{
    static void Main()
    {
        int order = 1000;
        int increment = 100;
        int rows = order / increment;
        string prefix = "ABC-";
        int startNumber = 1;
        int startRows = 1;

        for (int i = 1; i < rows   1; i  )
        {
            int rowIdx = startRows   (i - 1);
            int startIdx = startNumber   (increment * (i - 1));
            int endIdx = increment * i;

            System.Console.WriteLine($"{rowIdx}: {prefix}{startIdx} - {prefix}{endIdx}", i);
        }
    }
}

I want to iterate every other row number however, like this:

1A: ABC-1 - ABC-100
1B: ABC-101 - ABC-200
2A: ABC-201 - ABC-300
2B: ABC-301 - ABC-400
3A: ABC-401 - ABC-500
3B: ABC-501 - ABC-600
4A: ABC-601 - ABC-700
4B: ABC-701 - ABC-800
5A: ABC-801 - ABC-900
5B: ABC-901 - ABC-1000

It took me a few hours (trying not to look at tutorials/docs) to get the range to iterate, but getting the rows to "skip" stumped me completely.

Am I missing something obvious?

CodePudding user response:

You should look into C#'s Remainder operator - enter image description here

  •  Tags:  
  • c#
  • Related