Recently, when developing a calculator program, I found myself using goto
multiple times to restart a for loop.
Example:
StartLoop:
for (int i = 0; i < length; i++)
{
if (items[i] == condition)
{
//Do something
goto StartLoop:
}
}
I know that goto
should be avoided but what other way would I have to restart the loop?
Just set the value of i
:
int length = 9;
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
if (i == 7)
{
i = -1;
}
}
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
0
1
2
3
4
5
...