For ... in ... Loop

I am trying to run this simple code to display numbers on Serial monitor but I am getting the following error:

expected ‘(’ before ‘x’

Can anyone tell me what I am doing wrong please

int x;

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);

}

void loop() {
  // put your main code here, to run repeatedly:

 
  for x in  ( 1, 2, 3, 7, 15 )
  {
    Serial.println (x);
    delay (100);
  }

}

Can anyone tell me what I am doing wrong please

You’re inventing your own syntax for C++

Can anyone tell me what I am doing wrong please

Trying to use syntax that is not C++

You can do what you appear to be trying to do by putting the digits in an array and iterating through it using a for loop

Like this:

int myArray[] = {1, 2, 3, 7, 15};

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

void loop() {
  for (auto x : myArray )  {
    Serial.println(x);
  }
  delay(1000);
}

Thank you so much to everyone. Your feedback has been truly appreciated.

Erik_Baas:
Like this:

int myArray[] = {1, 2, 3, 7, 15};

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

void loop() {
 for (auto x : myArray )  {
   Serial.println(x);
 }
 delay(1000);
}

Many thanks to you.
With your code, I have just learned something new today.

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