For Loop and Serial.Print not working

Basically, when using a For loop, Serial.print is only printing at the end. Tried this on a Nano and a Mega. Using IDE 1.8.19.

From the code below, I get this in the serial monitor:
Hi
h: 4
k: 6

void setup()
{
  Serial.begin(9600);
  Serial.print("\r\nHi");
  Read();
}

void Read()
{
  byte h;
  byte k = 6;

  for (h = 0; h < 4; h++);
  {
    if (h == 2) k++;
    
    Serial.print("\r\n h: ");
    Serial.print(h);
    Serial.flush();
  }

  Serial.print("\r\n k: ");
  Serial.print(k);
}

void loop()
{
}

Welcome to the forum

What did you expect the code to print ?

a delay of few milliseconds might work.

loose the semi colon at the end of the for loop (you basically have an empty loop otherwise)

2 Likes

I knew it would be such a simple problem that I couldn't see it. Thanks.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.