My problem arises when I want to use the variable i after the exit of the for loop. I would assume that i is reading 7, the 8th increment in zero based counting but it actually reads as 8!!! One more increment to variable i has been made on loop exit.

In order to remedy this I would have to do something like i-- at the end of the loop and before using it in any further code. This seems to me to make the code harder to understand.

Is there a better solution?

网友答案:

When i is 7, the condition i < 8 is still fulfilled, so there is no reason to exit the loop.

It is not very clear to declare the loop variable before the loop and use it afterwards anyway. Rather, consider declaring the loop variable with the loop statement.

If using numIterations-1 really bothers you, you could also instead use int maxCounter = 7 and use i <= maxCounter instead as loop invariant.

网友答案:

When you say

for( i = 0; i < 8; i++ )

the middle part, i < 8, is the loop invariant: as long as it holds, there will be a next iteration. Therefore once you leave the loop, it is because the invariant holds no longer and i cannot possibly be less than 8.

The loop exits when i == 8, because the loop condition says: keep iterating as long as i < 8. Or to put it another way: stop iterating when i >= 8.

So the last i++ inside the loop will assign 8 to i, exiting the loop. That explains the results you're obtaining, and they're as expected. Perhaps you incorrectly assumed that the last i++ isn't executed before exiting the loop.

To obtain the behavior you want, try this instead - the intent of the code is clearer:

In this code is of course useless to add more logic because you already know that at the end of the loop the variable will always be the same. I think you need it in a more complex code with conditionals statements, so in that case you can do this:

int i = 0;
System.out.printf( "\n%d\n\n", i ); // show variable i before loop
for( i = 0; i < 8; i++ )
{
if ( i == 3 ) // just for example
break;
System.out.printf( "%d\t", i );
}
System.out.printf( "\n\n%d\n", i ); // show variable i after loop

and this will print:

0
0 1 2 3
3

网友答案:

This is really simple if you know the order of operation

STEP_1 - Action before body operation.

SETP_2 - Evaluate logical expression that continue operation if true.

STEP_3 - Perform body operation.

STEP_4 - Action after body operation.

So:

for(STEP_1; STEP_2; STEP_4) {
STEP_3
}

Usually in step 1 we initialize our variable and in step 4 we modify it for next evaluation of loop.

int i = 0;
for(; i< 8; i++) {
}

The loop breaks when i == 8, as is declared before loop the value stay.

网友答案:

In the loop the variable i is incremented until it reaches 8 or more at which point the condition triggers leaving the loop.

This is just the way it works.

So you have two options that I can think of.

subtract one from the variable i after the loop.

put an if statement in the loop so that when i equals 7, it does a break.

for (i = 0; i < 8; i++) {
if (i == 7) break;
}

Another option would be do use a do while as in:

i = -1;
do {
i++; // increment i so that it is at the correct current value starting with zero
// loop stuff
} while (i < 7);