i++ mystery

A for loop is a basic way of iterating through things.

You declare one like this:

For(int iterator; iterator = 0; iterator++)

You have to declare iterator (or i) first, it would be the same as saying 'int i;' somewhere else in the program before the loop. Then, you give it an initial value of 0. Then it will start counting from zero til wherever it's stop is.

i++ is shorthand for i = i + 1.

Your iterator can be named whatever you want it to be. It is just a variable that is assigned to start somewhere, and increments based on a rule.

So when you see for (int i; i=0;i++)

Its saying okay, we make an iterator and name it i, i will start from zero, and proceed by the pattern i = i + 1 for each iteration of our for loop, each execution of the body contained in it, meaning the first iteration, i is zero, then it's 1, then it's 2, 3, 4, so on.

/r/cs50 Thread