Nikola Matovic
Nikola Matovic

Reputation: 23

For-Loop ist doing weird things

My For-Loop ist doing weird things. When I debug my programm, the count of the loop is after the 4rd loop equals 0, after that it starts again.

If I don't debug my programm I'm getting an Stackoverflow-Exception.

for (int i = 0; i < gegnerischeFigurenn.Count; i++)
{
                gegnerischeAngriffsfelderr.AddRange(GetAvailableFields(gegnerischeFigurenn[i], schachbrett));
                Console.WriteLine("Count: " + i);
}

the console output is:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 0
Count: 1
Count: 2
Count: 3
Count: 0
Count: 1 ......

the result is an stackoverflow exeption if I don't debug the programm and else an wrong number increment of my count.

Upvotes: 0

Views: 81

Answers (1)

Kes Walker
Kes Walker

Reputation: 1493

This is likely to be because your method is calling itself from within itself (recursive call). If you were to share more of your code around the for-loop, that may be helpful but check for anywhere within your method which makes a reference to itself. This link explains how stackoverflow exceptions occur so may be helpful for you.

Upvotes: 2

Related Questions