7

So I have these few lines of code:

string[] newData = File.ReadAllLines(fileName)
int length = newData.Length;
for (int i = 0; i < length; i++)
{
    if (Condition)
    {
       //do something with the first line
    }
    else
    {
      //restart the for loop BUT skip first line and start reading from the second
    }
}

I've tried with goto, but as you can see, if I start the for loop again, it'll start from the first line.

So how can I restart the loop and change the starting line(getting different key from the array)?

Kirev
  • 147
  • 2
  • 2
  • 10
  • If condition is true do you only want to read the first line, and if condition is false then only read from line 2 to the end? – Steve May 28 '12 at 16:18
  • 2
    Also note your `i <= newData.Length` should be `<`. – Rawling May 28 '12 at 16:21
  • If it's true, I'm doing some job there with that first line and then with the second and etc. – Kirev May 28 '12 at 16:30

5 Answers5

12

I'd argue that a for loop is the wrong type of loop here, it doesn't correctly express the intent of the loop, and would definitely suggest to me that you're not going to mess with the counter.

int i = 0;
while(i < newData.Length) 
{
    if (//Condition)
    {
       //do something with the first line
       i++;
    }
    else
    {
        i = 1;
    }
}
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • 2
    But this is just an ugly formatted for loop .... Agreed that the OP should shop for something else though. – H H May 28 '12 at 16:20
  • 2
    No, it's a while loop. Yes you could do it with a for loop, but I'd consider it a code smell. For _implies_ doing something a number of times, or for a number of steps. That's not what this loop does. – Binary Worrier May 28 '12 at 16:23
  • 1
    +1, cuz I'm no deaf! :) for loop that goes back is an awful code. – gdoron May 28 '12 at 16:27
  • 8
    @Henk: I don't think you're hearing me. It doesn't matter (and I don't care) that the two loops are equivalent, when some one reads the code they're likely to dismiss the `for` version as looping a number of times without catching the buried change to `i`. I'd argue it's the apparent simplicity of the `while` condition is what makes it a flag to the reader to check that this isn't as simple as it looks. IMHO the structure in the `for` version doesn't count for squat as you're subverting `for` and it's structure by breaking the most common use case of `for`. – Binary Worrier May 28 '12 at 16:41
  • This answer was the solution in my case. Thank you! – Kirev May 29 '12 at 02:11
  • @Kirev: Glad to be of help pal :) – Binary Worrier May 29 '12 at 10:57
8

Just change the index of the for loop:

for (int i = 0; i < newData.Length; i++) // < instead of <= as @Rawling commented.
{
    if (//Condition)
    {
       //do something with the first line
    }
    else
    {
      // Change the loop index to zero, so plus the increment in the next 
      // iteration, the index will be 1 => the second element.
      i = 0;
    }
}

Note that this looks like an excellent spaghetti code... Changing the index of a for loop usually indicate that you're doing something wrong.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • @Rawling. Didn't get you last comment. what did you mean? – gdoron May 28 '12 at 16:25
  • 1
    +1 for spaghetti. Have argued a for loop isn't appropriate here, but it seems to be falling on deaf ears :) – Binary Worrier May 28 '12 at 16:25
  • Yup, but if it fails on the second line, it'll continue reading the second one and going to the 3, 4, etc. Basically I need to read every next line if condition fails. Like: first line is newData[0], second is newData[1] I need to restart the loop and change the newData[i] every time. PS: Sorry for my bad English – Kirev May 28 '12 at 16:27
  • Rescinded means taken back/removed :) – Rawling May 28 '12 at 16:27
  • And yeah. I think it's big mistake to use for loop here. I'll try to fix that. – Kirev May 28 '12 at 16:27
4

Just set i = 0 in your else statement; the i++ in the loop declaration should then set it to 1 and thus skip the first line.

Rawling
  • 49,248
  • 7
  • 89
  • 127
0
string[] newData = File.ReadAllLines(fileName)

for (int i = 0; i <= newData.Length; i++)
{
    if (//Condition)
    {
       //do something with the first line
    }
    else
    {
      //restart the for loop BUT skip first line and start reading from the second
      i = 0;
    }
}
gdoron
  • 147,333
  • 58
  • 291
  • 367
Hiren Visavadiya
  • 485
  • 1
  • 3
  • 15
0

You would just reset i and resize the array

int length = newData.Length; // never computer on each iteration
for (int i = 0; i < length; i++)
{
    if (condition)
    {
       //do something with the first line
    }
    else
    {
      // Resize array
      string[] newarr = new string[length - 1 - i];
      /*
       * public static void Copy(
       *    Array sourceArray,
       *    int sourceIndex,
       *    Array destinationArray,
       *    int destinationIndex,
       *    int length
       * )
       */
      System.Array.Copy(newData, i, newarr, 0, newarr.Length); // if this doesn't work, try `i+1` or `i-1`
      // Set array
      newData = newarr;
      // Restart loop
      i = 0;
    }
}
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74