Why for loop is acting like while loop in C# Arrays?

Viewed 37

this for loop inside arrays is working like while and printing only the second index. what should i do to make it print only even indexes? enter image description here

1 Answers

You have inverted the += operator in your for loop. You're saying i = +2 which is the same as i = 2

The correct code would be:

for (int i = 0; i < cars.Length; i += 2)
{
    Console.WriteLine(cars[i]);
}
Related