do while loop not stopping blink

Hello,

I wanted to learn about while and do while loops. No problem with a simple "Blink" while loop, but the do while doesn't stop after the index goes over the condition. Seems simple.. why won't it stop?

/*
Simple DO While Loop
*/
int loopIndex = 0;

void setup()
  {
    pinMode(13, OUTPUT);
    
    Serial.begin(9600);
  }  
void loop()
  {
    do
    {
    Serial.print("loopIndex 1: ");
    Serial.print(loopIndex);
    Serial.println(" ");
    
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
    
    loopIndex = loopIndex + 1;
    
    Serial.print("loopIndex 2: ");
    Serial.print(loopIndex);
    Serial.println(" ");
    
    }while(loopIndex < 4);
    
    
  }

Thanks for any help or suggestions.
Steve.

Because it is in "loop()" which gets called over and over again.

AWOL:
Because it is in "loop()" which gets called over and over again.

Actually, the "problem" is in the nature of the do/while loop (which is why I NEVER use that construct).

The body of the do/while statement is executed, and THEN the condition is checked. What is happening is that on the first call to loop(), the do/while body is executed 4 times. Then, it ends, and loop() is called again. The body of the statement is executed, and the while conditions then says "Oops, that's enough of that", and the do/while ends, and loop() is called again.

The moral of the story? Don't use do/while.

All the replies are right, in my opinion. But I think it's missed 1 example. Try this:

/*
Simple DO While Loop
*/
int loopIndex = 0;

void setup()
  {
    pinMode(13, OUTPUT);
    
    Serial.begin(9600);

    do
    {
       Serial.print("loopIndex (enter loop):");
       Serial.println(loopIndex);
    
       digitalWrite(13,HIGH);
       delay(1000);
       digitalWrite(13,LOW);
       delay(1000);
    
       loopIndex++;
    
       Serial.print("loopIndex (exit loop): ");
       Serial.println(loopIndex);
    
    } while(loopIndex < 4);
  }  

void loop()
  {
  }

I made a few minor changes. Do you understand it?

The moral of the story? Don't use do/while.

That may be too dogmatic. The key to keep in mind is that a do-while always makes at least one pass through the loop regardless of the test condition(s) at the bottom of the loop. A regular while loop may have the test expression such that the statements in the while statement block are never executed.

PaulS:
The moral of the story? Don't use do/while.

Huh? What bizarreness is this?

Its a bag of tools, use the right tool for the job in hand. do-while loops
are rarer than while or for loops, but sometimes they are just right.

The problem here is that the do-while loop belongs in setup(), not loop().

Huh? What bizarreness is this?

I've been writing C/C++ code for a living for 25+ years. I've never used a do/while loop. I can't imagine a situation where I'd want the body of the statement executed once unconditionally, and then 0 or more times conditionally. IF that situation ever arose, comments, some code, and a while loop would make orders of magnitude more sense. The code would be in a function, too.