My MsTimer2 isn't work;; helpme

hi
i have some problem in my MsTimer code

#include <MsTimer2.h>

int ex_time = 0;
int now_time = 0;

void setup() {
  Serial.begin(9600);
  MsTimer2::set(100, timer);
  MsTimer2::start();

}

void loop() {
  ex_time = now_time;
  while((now_time - ex_time) <= 70) {

  }

  Serial.println("HI");

}

void timer(void) {
  //Serial.println(now_time);
  //Serial.println(ex_time);
  now_time++;
}

this code does not print HI,

#include <MsTimer2.h>

int ex_time = 0;
int now_time = 0;

void setup() {
  Serial.begin(9600);
  MsTimer2::set(100, timer);
  MsTimer2::start();

}

void loop() {
  ex_time = now_time;
  while((now_time - ex_time) <= 70) {
    Serial.print("");
  }

  Serial.println("HI");

}

void timer(void) {
  //Serial.println(now_time);
  //Serial.println(ex_time);
  now_time++;
}

But this code prints the HI!!

Why doesn't the first code work?

Both the first and second codes confirm that the timer is working.

I think doesn't work loop or while in first code, but i'm not sure.

Can you tell me why this is happening?

Thanks

Why doesn't the first code work?
Can you tell me why this is happening?

volatile int now_time = 0;
//int now_time = 0;

now_time is incremented within a timer interrupt and then used back in loop(). It needs to be declared as volatile, so the compiler recognizes that it can change.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/volatile/

The compiler tries to "optimize" the code as it converts it from c++ to the assembly language which actually run on the processor. The compiler tries to minimize code size, and eliminate things from the code that won't occur. The empty while() loop with the non-volatile variable has resulted in compiler optimization.

You would need to look at the compiled code to understand exactly what the compiler has done with your code in each of the two examples.