Statement required twice for sketch to work

Could someone explain why this statement has to be included twice for this sketch to work please?
The sketch just takes a character input from the USB port -using serial monitor- echos it back to the screen and displays its value on the leds -in decimal or hex.

Duplicate code highlighted in the code window like this....

//+++++++++++++++++++++++++++++++++++++++
countNow = SerialInVal; // +
//+++++++++++++++++++++++++++++++++++++++

Exactly the same statement is made in the code about 5 lines above it.

But without this extra one, the leds always display 0.
I am aware that countNow is destroyed by the subsequent code. I have taken this from another sketch i was using.

/* JR 15/1/2012
  Take a character from USB port and write its value to  HP5082-7340  LED displays.
*/

//++++ found defining these where they are used in the program saves memory
//byte state;     // flag
//byte selectHex; // 0 display in decimal , 1 display in hex
//long lsn;       // least ignificant nibble  - lttle endian
int outPin;

int outPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };   
// array of pin numbers to which LEDs are attached
// pins 2,3,4,5 are display data bits 0,1,2,3
// pin  6 (Pwm)is blanking  (1 = blank)
// pin  7 is display latch  (1 = latch)


long countNow;
int SerialInVal;



void setup() 
{
  // loop over the pin array and set them all to output:
  for (int pin = 0; pin < 10; pin++) 
  {
    pinMode(outPins[pin], OUTPUT); 
  }

  analogWrite(6,100);     // lower value = brighter display -
                          // (these displays consume a lot of current)
  pinMode(A5,INPUT);      // Pushbutton on A5 input -
  digitalWrite(A5,HIGH);  // used for selecting Decimal or Hex display

  Serial.begin(9600);

}// end of void setup


void loop() 
{
  byte selectHex = !digitalRead(A5);// read state of pushbutton and invert it

  if (Serial.available())
  {

    SerialInVal = Serial.read();

    Serial.print(SerialInVal);
    Serial.print("\t");              // prints a tab
    countNow = SerialInVal; 
    Serial.print(char(countNow));
    Serial.print("\t");              // prints a tab
    Serial.print(char(countNow-1));
    Serial.println("");


  }//end of if serial available


//+++++++++++++++++++++++++++++++++++++++++++++++++++++
 countNow = SerialInVal; //                           +
//+++++++++++++++++++++++++++++++++++++++++++++++++++++  
 

  long lsn = countNow;   
  long count = countNow;

  for (int pin = 7;pin < 12; pin++)
  { 
    if (selectHex == 0)
    {  //++++++++++++++++++++++++++ convert 'lsn' to bcd for decimal display 
      long temp = countNow / 10;
      lsn = countNow - (temp * 10);
      countNow = temp;
    } 
    //+++++++++++++++++++++++++++ otherwise, just output 'lsn' in hex

    // output data to display  
    for (int bitValue = 0; bitValue < 4;bitValue++)
    { 
      outPin = bitValue + 2; //pin is bit position + 2
      byte state = lsn & 1;
      digitalWrite( outPin,state);
      lsn = lsn >> 1;
    } 

    // write to the relevent display
    digitalWrite (pin, LOW);  //enable display to read data
    digitalWrite (pin, HIGH); //latch data   

  }



}//end of void loop

The first "countNow = SerialInVal;" only gets executed when Serial.available() is true.

The second one gets executed every time through loop(), even if no new character has arrived. It re-sets countNow to match SerialInVal, the value of the last character received. As you say, countNow is destroyed later in the code and I guess your code expects it not to be.

Ahh!

Yes. I've twigged now.

So the leds probably do display the data briefly, for one scan.
Then because countNow is not preserved the leds subsequently display whats left of it.
0.

Thanks again.