I have a property that I'll call index. I have a mutable array that I'll call array. I'm shocked to find this code throws an index out of bounds exception?
if(index >= [array count]) return;
for(self.item = [array objectAtIndex:index]; index < [array count]; self.item = [array objectAtIndex:index]) {
index++;
//do stuffs
}
However, this variant works:
if(index >= [array count]) return;
while(index < [array count];) {
self.item = [array objectAtIndex:index];
index++;
//do stuffs
}
I expect for loops to operate like so:
for(initialization instructions; condition; next iteration instruction) {...}
I expect the following sequence:
- The initialization instructions are executed
- Execute code in for loop
- Break if condition returns false/0. otherwise, execute next iteration instruction. Then go to 2.
This tells me the for loops do not necessarily check the condition prior to executing the next iteration code (as is in C/C++). So, I'm curious whether there are multiple schools of thought on the order of operations of the for loop. If not, this tells me I have more complicated issues to address.
Thanks.