New to coding, question on populating an array with for a loop

As stated, I am new to programming in general, so sorry if this question is a waste of time.

I am playing around with for loops and arrays, and I tried to populate an array using a for loop. Pretty simple, worked without any problems (original code below)

int[] items = new int[10];

for (int i = 0; i < 10; i++) {

items[i] = i;

}

for (int i = 0; i < 10; i++) {

Console.WriteLine(items[i]);

}

While tinkering around, changing things here and there, I discovered that, if I make “i” increment inside of the first for loop, i get two zeros before the expected numbers (see below)

for (int i = 0; i < 10; i++) {

items[i] = i++;

}

//output

0

0

2

0

4

0

6

0

8

0

Can someone explain to my small brain why the zeros “appear?” Something to do with incrementing “i” a second time i guess, but I don’t understand why that would feed me zeros again.

submitted by /u/stovethehuman
[link] [comments]

Leave a Reply