For Loops

I think that I have either misunderstood the purpose of For loops or have made a blindingly obvious error that I simply cannot see.

My understanding is that an operation will be carried out for as long as the test condition is true.

In my example below, I expected the words Great Auk to be printed ten times only. However, it prints indefinitely.

void setup() {
  Serial.begin(9600);
}

void loop() {
{  
    for (int x = 0; x <=10; x++){
      Serial.println ("Great Auk");
          }
}

}

I have tried different combinations of logic in the condition, but to no avail. I should appreciate any pointers.

Thank you.

James.

Your for is also inside loop() remember, so the for does a small number but then the whole thing starts over.

Put it in setup() and the for will only do its thing once.

catceefer:
I think that I have either misunderstood the purpose of For loops or have made a blindingly obvious error that I simply cannot see.

My understanding is that an operation will be carried out for as long as the test condition is true.

In my example below, I expected the words Great Auk to be printed ten times only. However, it prints indefinitely.

Yes, of course, that’s the intention of the void loop() function.

As soon as the loop() function has anded or you leave it, it is restarted and runs again and again and again.

If you want things to happen once (i.e. execute once through a for-loop), put it in setup()!

If you want things happen again and again and again, put it in loop()!

Tot make that clear, try

void setup() {
  Serial.begin(9600);
}

void loop() {
{ 
    for (int x = 0; x <=10; x++){
      Serial.println ("Great Auk");
          }
    Serial.print(”next loop!");
}

}

Thank you for the prompt replies. I had another look after I had posted this and twigged what was happening. I was, as I suspected, missing the obvious.

James.

If you'd printed the value of x, you'd've got there quicker.