c# restart for loop
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;
}
}
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.
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.