Reference.Increment History

Show minor edits - Show changes to markup

July 16, 2007, at 04:35 PM by David A. Mellis -
Added line 34:
July 16, 2007, at 04:35 PM by David A. Mellis -
Changed line 13 from:

x-- ; // decrement x by one and returns the old value of x @]

to:

x-- ; // decrement x by one and returns the old value of x

July 16, 2007, at 04:35 PM by David A. Mellis - clarifying x++ and ++x, etc.
Changed lines 10-12 from:

x++ ; // equivalent to the expression x = x + 1; x-- ; // equivalent to the expression x = x - 1; @]

to:

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 @]

Changed lines 18-19 from:

x: any variable type

to:

x: an integer or long (possibly unsigned)

Changed lines 22-23 from:

Incremented or decremented variable

to:

The original or newly incremented / decremented value of the variable.

Changed lines 27-30 from:

x++; // x now contains 3 x--; // x contains 2 again@]

to:

y = ++x; // x now contains 3, y contains 3 y = x--; // x contains 2 again, y still contains 3 @]

Deleted lines 31-32:
Deleted line 33:
July 16, 2007, at 12:18 PM by Paul Badger -
July 16, 2007, at 12:17 PM by Paul Badger -
Changed line 23 from:

[@x = 2; // x now contains 2

to:

[@x = 2;

July 16, 2007, at 12:17 PM by Paul Badger -
Changed lines 24-27 from:

x++; // x now contains 3 x--; // x contains 2 again@]

to:

x++; // x now contains 3 x--; // x contains 2 again@]

July 16, 2007, at 12:16 PM by Paul Badger -
July 16, 2007, at 12:16 PM by Paul Badger -
Deleted lines 4-7:

The ++ operator is

The -- operator is equivalent to the expression x = x - 1;

July 16, 2007, at 12:14 PM by Paul Badger -
Changed lines 3-4 from:
Description
to:

Description

July 16, 2007, at 12:14 PM by Paul Badger -
Changed line 35 from:
to:

+=\\

July 16, 2007, at 12:13 PM by Paul Badger -
Changed lines 35-36 from:
to:
July 16, 2007, at 12:08 PM by Paul Badger -
Deleted lines 26-28:

Deleted lines 31-63:

Example Code

// 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

Tip

the modulo operator will not work on floats

July 16, 2007, at 12:07 PM by Paul Badger -
Added line 13:

[@

Changed lines 15-17 from:

x-- ; // equivalent to the expression x = x - 1;

to:

x-- ; // equivalent to the expression x = x - 1; @]

July 16, 2007, at 12:06 PM by Paul Badger -
Added line 14:
July 16, 2007, at 12:06 PM by Paul Badger -
Changed lines 1-8 from:

++ (increment)

Increment a variable

Syntax

x++;

to:

++ (increment) / -- (decrement)

Description

The ++ operator is

The -- operator is equivalent to the expression x = x - 1;

Increment or decrement a variable

Syntax

x++ ; // equivalent to the expression x = x + 1; x-- ; // equivalent to the expression x = x - 1;

Parameters

x: any variable type

Returns

Incremented or decremented variable

Examples

x = 2;    // x now contains 2
x++;        // x now contains 3
x--;        // x contains 2 again

Example Code

// 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

Tip

the modulo operator will not work on floats

See also

division

July 16, 2007, at 11:51 AM by Paul Badger -
Added lines 1-8:

++ (increment)

Increment a variable

Syntax

x++;

Share