increment

there are two ways to increment a variable.

x++ and ++x

reading this : https://www.arduino.cc/en/Reference/Increment

it says that

++x will increment x by 1 and show that value. but x++ will show the initial value of x ..

if that is the case, then when, or where in the loop, does x become incremented ?

Well in a loop you usually have this

for (int i = 0; i<10; [b]i++[/b]) {}

or

for (int i = 0; i<10; [b]++i[/b]) {}

so the increment is a statement by itself, so it does not matter

but if you do

int a,b,i;

i = 5;
a = i++; // a is 5, i is 6
b = ++i; // b is 6, i is 7

The two forms determine the order of execution.

++X means increment the value of X then use it's new value.

X++ means use present value of X then increment it's value.

The notation is two operations with variable order.

++x will increment x by 1 and show that value. but x++ will show the initial value of x ..

Poor phrasing. Each operation [u]returns[/u] a value. See http://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept

Often with questions like this, it’s easiest to write a short program and find out yourself. Using J-M-L’s example:

void setup() {
  int a,b,i;

  Serial.begin(9600);
  
  i = 5;
  a = i++;              // Confirm that a receives the value of i before the increment
  Serial.print("a = ");
  Serial.print(a);
  Serial.print("   i = ");
  Serial.println(i);
  
  i = 5;                // reset i
  b = ++i;              // Confirm that b receives the value of i after the increment
  Serial.print("b = ");
  Serial.print(b);
  Serial.print("   i = ");
  Serial.println(i);

}

void loop() {

}

thanks.

it appears that the value is changed at the end of the line. thanks for the idea of doing a simple sketch

void setup() {
 int a,b,i=5;
 Serial.begin(9600);

Serial.println (i++);       // prints 5 = value of i before incrementing
Serial.println (i);         // prints 6
i++;          
Serial.println (i);         // prints 7 = value because increment has occured 
a = i++; 
Serial.println (i);         // prints 8
b = ++i; 
Serial.println (i);         // prints 9
Serial.println (b=++i);     // prints 10 = value of equation

}

void loop() {
}



}

void loop() {

}

jremington: Poor phrasing. Each operation [u]returns[/u] a value. See http://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept

thanks for the link. what I was missing was what was termed sequence point in that link.

in my example, it would be ; at the end of the line.

in the link they offer :

(i++ > 0) && someProperty(myArray[i])

and offer that && becomes a sequence point at which time i has been incremented.