0

I want to run a for loop where its initialiser and condition are based on the values of an integer array. for eg:-

 int[] array = [0,5,8,12..]

    for(initialiser = array[0], condition < array[1], initialiser++)
    {
        //program condition is satisfied.
        break;
    }

in the first looop initialiser is array[0] and condition is array[1]
in the second loop initialiser is array[1] and condition is array[2]
and so on array lenght is not fixed.So far i am unable to make it work efficiently. This is what is have tried.

            int idx = 0;
            foreach (int id in oId)
            {
                if (id != oId.Last())
                {
                    for (int k = 1; k < oCnt; k++)
                    {
                        for (int j = id; j < oId[k]; j++)
                        {
                            //Logic condition is satisfied vId is an array here
                        }
                    }
                    r[idx++] = vId[0];
                }
                if (id == oId.Last())
                {
                    for (int j = oId.Last(); j < totalLength; j++)
                    {
                        //Logic condition is satisfied vId is an array here
                    }
                    r[idx++] = vId[0];
                }
            }
n00b
  • 181
  • 1
  • 1
  • 18
  • _".. array length is not fixed."_ - array length is always fixed. – SᴇM Jan 12 '21 at 05:57
  • array length is variable. – n00b Jan 12 '21 at 05:58
  • So the first iteration you want the loop to run from `0 to 5` and the second iteration you want to run from `5 to 8` and so forth based on the integer array ? – TheGeneral Jan 12 '21 at 06:02
  • if the `condition` is `array[1]` in the first loop, the `condition < array[1]` will not work. What is the actual problem you're trying to solve? Seems like [XY Problem](https://xyproblem.info/) to me. – SᴇM Jan 12 '21 at 06:07
  • yes this is the gist. – n00b Jan 12 '21 at 06:07
  • Note that a lot of the methods in the duplicate are allocatey and inefficient or very outdated, a few of them are ok though. – TheGeneral Jan 12 '21 at 06:50
  • Ideal and more generic approach is in this answer: [https://stackoverflow.com/a/1581482/1565525](https://stackoverflow.com/a/1581482/1565525), ReactiveExtensions have dedicated method for this: [https://stackoverflow.com/a/15380260/1565525](https://stackoverflow.com/a/15380260/1565525) – Fabio Jan 12 '21 at 07:01

1 Answers1

3

Maybe you are looking for:

var array = new []{0,5,8,12};

for (var i = 0; i < array.Length-1; i++)
{
   for (var j = array[i]; j < array[i + 1]; j++)
   {
      Console.Write(j + " ");
   }
   Console.WriteLine();
}

Output

0 1 2 3 4
5 6 7
8 9 10 11

You could also make yourself an iterator method

public static IEnumerable<(int start, int finish)> GetRange(int[] source)
{
   for (var i = 0; i < source.Length - 1; i++)
      yield return (source[i], source[i + 1]);
}

Which would allow more enumerable flexibility

var results = GetRange(array)
   .Select(x => string.Join(", ", Enumerable.Range(x.start, x.finish-x.start)));

foreach (var result in results)
   Console.WriteLine(result);

Output

0, 1, 2, 3, 4
5, 6, 7
8, 9, 10, 11
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141