Hide minor edits - Show changes to markup
x-- ; // decrement x by one and returns the old value of x @]
x-- ; // decrement x by one and returns the old value of x
x++ ; // equivalent to the expression x = x + 1; x-- ; // equivalent to the expression x = x - 1; @]
x++; // increment x by one and returns the old value of x ++x; // increment x by one and returns the new value of x
x-- ; // decrement x by one and returns the old value of x @] --x ; // decrement x by one and returns the new value of x @]
x: any variable type
x: an integer or long (possibly unsigned)
Incremented or decremented variable
The original or newly incremented / decremented value of the variable.
x++; // x now contains 3 x--; // x contains 2 again@]
y = ++x; // x now contains 3, y contains 3 y = x--; // x contains 2 again, y still contains 3 @]
[@x = 2; // x now contains 2
[@x = 2;
x++; // x now contains 3 x--; // x contains 2 again@]
x++; // x now contains 3 x--; // x contains 2 again@]
The ++ operator is
The -- operator is equivalent to the expression x = x - 1;
// check a sensor every 10 times through a loop
void loop(){
i++;
if ((i % 10) == 0){ // read sensor every ten times through loop
x = analogRead(sensPin);
}
/ ...
}
// setup a buffer that averages the last five samples of a sensor
int senVal[5]; // create an array for sensor data
int i, j; // counter variables
long average; // variable to store average
...
void loop(){
// input sensor data into oldest memory slot
sensVal[(i++) % 5] = analogRead(sensPin);
average = 0;
for (j=0; j<5; j++){
average += sensVal[j]; // add up the samples
}
average = average / 5; // divide by total
the modulo operator will not work on floats
[@
x-- ; // equivalent to the expression x = x - 1;
x-- ; // equivalent to the expression x = x - 1; @]
Increment a variable
x++;
The ++ operator is
The -- operator is equivalent to the expression x = x - 1;
Increment or decrement a variable
x++ ; // equivalent to the expression x = x + 1; x-- ; // equivalent to the expression x = x - 1;
x: any variable type
Incremented or decremented variable
x = 2; // x now contains 2 x++; // x now contains 3 x--; // x contains 2 again
// check a sensor every 10 times through a loop
void loop(){
i++;
if ((i % 10) == 0){ // read sensor every ten times through loop
x = analogRead(sensPin);
}
/ ...
}
// setup a buffer that averages the last five samples of a sensor
int senVal[5]; // create an array for sensor data
int i, j; // counter variables
long average; // variable to store average
...
void loop(){
// input sensor data into oldest memory slot
sensVal[(i++) % 5] = analogRead(sensPin);
average = 0;
for (j=0; j<5; j++){
average += sensVal[j]; // add up the samples
}
average = average / 5; // divide by total
the modulo operator will not work on floats
Increment a variable
x++;