Of all the answers so far, I think that Charlie74 is closest to reasoning the same way I do.
The for loop consists of three segments:
for (initialization; termination; increment) {
statement(s)
}
it is my opinion that only things that affext the termination should be part of the increment.... In other words, if the value is not part of the termination condition then it should not be part of the increment.
In this case, the count does not affect the termination condition, and as a result it does not belong in the increment section..
For what it's worth, there are a couple of things I would do differently still:
- you have a typos in your second example.... (missing
x
in x <
and missing y <
) [This has been fixed in the question now.]
- I would include the count++ as part of the array assignment (which is where I think it belongs):
for(int x = 0; x < max_x_index; x++){
for(int y = 0; y < max_y_index; y++){
some2Darray[count++] = ...;
}
}
Discussion on ++count
vs. count++
in this context
is this one of those instances where if you change i++
to ++i
it will change what is assigned in the array? could you explain that a little more ... ?
Here goes.... Yes, this is one of the situations where the the pre-increment or post-increment difference is significant. Wikipedia has a section on this.
The pre-increment operator ++count
increments the count variable. The post-increment operator count++
also increments the count variable. The difference is that while both ++count
and count++
are expressions, the resulting values of the expressions are different.
It is important to understand what it means that count++
is an expression. An expression is something that produces a value. In the context array[count] = 1
, the count
is an expression, it is something with a value. That expression could instead be a constant (array[0]
), a function (array[getNextIndex()]
, or even a more complex expression consisting of sub-expressions and operators (array[5 - 3]
).
So, count++
is an expression. It is also something that modifies the value in the count
variable. Similarly, ++count
is an expression, and it also modifies the value of count
. The difference between them is not what they do to count
, but it is the value of the expression.
With count++
the value of the expression is the value of count
before it is incremented.
With ++count
the value of the expression is the value of count
after it is incremented.
Thus, in the example above, compare two different situations:
int count = 0;
array[count++] = 1;
vs.
int count = 0;
array[++count] = 1;
In the first example, the value of the expression count++
is 0 (the value before the increment) and as a result, the value at index 0
of the array
is set to 1
. I.e. it is equivalent to:
int count = 0;
array[count] = 1;
count = count +1;
in the second example, the value of the expression ++count
is 1 (the value after the increment) and as a result, the value at index 1
of the array
is set to 1
. I.e. it is equivalent to:
int count = 0;
count = count + 1;
array[count] = 1;
It is obvious, that by using ++count
we will start adding data at index 1 instead of 0, and we run the risk of an out-of-bounds operation when we get to the end of the data.
In the case of adding data to an array (like what this question is doing), it makes sense to set a counter to the 'next' slot to use, and then do a post-increment so that you 'use' the next slot, and then increment it so it is pointing to the next slot again.
A bonus of doing things this way is that, at the end, the count
is also the number of slots in the array that you have used.
Serendipitously, another question came up ( Finding all indices of largest value in an array ) where one potential answer illustrates this point nicely.
some2Darray[count++]
is common in C, but I don't see it very often in Java. \$\endgroup\$ – Izkata Dec 8 '13 at 17:43some2Darray[count++]
is just as valid in Java as in C. There's no reason or evidence to suggest that it's less prevalent in Java. \$\endgroup\$ – 200_success Dec 8 '13 at 18:05