Counting?

Help please! If I have a variable called "a" and I want it to go from 1 to 10 and then come back to 1 and then to 20 and to 1 what do I have to do

int a = 1;

while (a < 10) a++;
while (a > 1) a–;
while (a < 20) a++;

Would be easier to use a variable maximum:

int max = 10;
void loop() {
  for (int i = 1; i <= max; i++) {
    //Do something
  }
  max = (max == 10) ? 20 : 10;
}

The for loop will alternate between running from 1 to 10 and 1 to 20 for each execution of “loop()”.

because it is hard to know what to search for,

 max = (max == 10) ? 20 : 10;

The ternary operator.

hey guys I am a newbie so how do I make a variable goes from 1 to 10 then reset it to 1 then 1 to 20?

Ask this guy

for God’s sake, this is the kind of thing any C/C**++** tutorial will tell you. while you’re there, do take the time to increment your knowledge in other areas of the language.

Threads merged.