how to works with previous and current

how to works with previous and current values?
thank you?

currentReading = reading from an input;
if (currentReading != previousReading)
  {
     //do some stuff because the reading has changed
     previousReading = currentReading;  // remember the reading as it is now for next time
  }