0

i've been searching for an answer, but i get more and more confused.

i have these 2 for loops

for (int i = 1; (i < 5) && move.from[i - 1]; i++) {
    int const departurePoint = move.from[i - 1];
    int arrivalPoint = move.to[i - 1];

    if (arrivalPoint < 0) { // A blot was hit
        arrivalPoint = -arrivalPoint;
        board[1 - turn][BAR - arrivalPoint]--; // Remove the blot
        board[1 - turn][BAR]++; // and place it on the bar */
    }

    board[turn][departurePoint]--; // Move our own checker
    board[turn][arrivalPoint]++; // to it's landing spot.
}

and

for (int i = 1; (i < 5) && move.from[i - 1]; ++i) {
    int const departurePoint = move.from[i - 1];
    int arrivalPoint = move.to[i - 1];

    if (arrivalPoint < 0) { // We hit a blot
        arrivalPoint = -arrivalPoint;
        board[1 - turn][BAR - arrivalPoint]++; // Replace the blot
        board[1 - turn][BAR]--; // remove it from the bar
    }

    board[turn][departurePoint]++; // Replace our own checker
    board[turn][arrivalPoint]--; // to it's original spot.
}

my questions are:

  1. In the for loop statement with pre-increment, has i been incremented when the "move.from[i - 1] is evaluated?
  2. Has i been incremented in the body of the statement?
helgovic
  • 1
  • 6
  • 3
    Possible duplicate of [Difference between i++ and ++i in a loop?](https://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop) – Jay Nov 11 '17 at 03:08

3 Answers3

2
for (int i = 1; (i < 5) && move.from[i - 1]; i++ /*i increments here and nowhere else*/)

and

for (int i = 1; (i < 5) && move.from[i - 1]; ++i /*i increments here and nowhere else*/)

Both codes are equivalent. The difference is very slight and it does not apply to this example.

when i==3,

++i means: 4=i+1=(++i) then i=4.

i++ means: 3= i =(i++) then i=4.

it does not make a difference until you assign it to another variable:

for(...; ...; k=i++)

or

for(...; ...; k=++i)

i+1 means:

store i to a temporary variable. Increase the temporary variable by one. It reads i but does not write to i. i will change only on ++, -- or i= and a few other cases.

Arash
  • 2,114
  • 11
  • 16
  • Okay. Wouldn't this be the same then? for (int i = 0; (i < 4) && move.from[i]; ++i) { int const departurePoint = move.from[i];... – helgovic Nov 11 '17 at 03:40
  • @helgovic, No. In your case, `++i` and `i++` are identical. The result of increment is not used. The condition `(i < 5)` is checked after the increment. Therefore, it does not care about the `i++` or `++i`. – Arash Nov 11 '17 at 04:10
2

Your short question is What is the difference between i++ and ++i is the value of the expression?

The value i++ is the value of i before the increment. The value of ++i is the value of i after the increment.

Example:

int i = 2;
std::cout << i++ << std::cout; // shows 2
std::cout << i << std::cout; // shows 3

i = 2;
std::cout << ++i << std::cout; // shows 3
std::cout << i << std::cout; // shows 3

The i-- and --i operators works the same way.

0xdw
  • 3,755
  • 2
  • 25
  • 40
0

The for loop takes two statements and an expresion like this:

for(init_statement;condition_expresion;progress_statement){
 //...body...
}

The semantic of this instruction is basically the same as writing:

init_statement;
while(condition_expresion){
  //...body...
  progress_statement;
}

Notice the side effect of both i++ and ++i is the same, the variable i is incremented by one. Those two instructions are different when they are used as expresions, i++ evaluates to the value of i before the increment, and ++i evaluates to the value after the increment. This is all irrelevant for the for loop, as the value of the progression statement is discarded.

jspurim
  • 925
  • 8
  • 25