Solved for loop like basic?

Coding

The C for loop is much more flexible than for loops found in some other computer languages, including BASIC. Any or all of the three header elements may be omitted, although the semicolons are required.

OK, of so, how do I write an example like: In Basic: for i = x to y step z ? in one loop.
Any of the 3 values can be + or -, x can be > y or < y.

The a good design tip for micro controller programming is DON'T use for or while loops!. As for how to use a fro loop just look in the reference section.

Mark

The a good design tip for micro controller programming is DON’T use for or while loops!.

couldnt live without my for() loops…

OK, of so, how do I write an example like: In Basic: for i = x to y step z ? in one loop.
Any of the 3 values can be + or -, x can be > y or < y.

not 100% sure what you mean, but like these?

void setup()
{
  int j = -1;
  //leave out increminting for for() statement:
  Serial.begin(115200);
  for (int i = -4; i <= 10;)
  {
    Serial.println(i);
    i++;
  }
  Serial.println(" ");
  //leave out limit:
  for (int i = -4;;i++)
  {
    Serial.println(i);
    if (i>10) break;
  }
  Serial.println(" ");
  //leave out initial value
  for(; j >= -10 ; j--)
  {
    Serial.println(j);
  }
}

void loop()
{

}
or (int i = -4;;i++)
  • or - Increminting should be automatic. Why specify ++ or -- The compiler should figure that out.

steinie44: or (int i = -4;;i++)

  • or - Increminting should be automatic. Why specify ++ or -- The compiler should figure that out.

How is the compiler supposed to know to count up or count down? You may want a deliberate wrap.

How is the compiler supposed to know to count up or count down?

By the sign of z.

This also works (I hope :slight_smile: )

for(int j = 21; j <= 70 ; j += 7)

…R

It does, I was doing the same

byte x;
byte a;

const byte blinkpin = 13;

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

void loop ()
{
  for(x=0; x < 10; x=x+2)
  {
    digitalWrite(blinkpin, HIGH);
    Serial.println(x,DEC);
    delay(500);
    digitalWrite(blinkpin, LOW);
    delay(500);
  }
  delay(2000);
}

holmes4: The a good design tip for micro controller programming is DON'T use for or while loops!. As for how to use a fro loop just look in the reference section.

Mark

Nope, I disagree. Writing pinModes for a Mega gets tedious, for one thing.

for(x=0; x < 10; x=x+2)

How about

for(i=x; i < y; i=i+z)

But what if x > y?

steinie44:

for(x=0; x < 10; x=x+2)

How about

for(i=x; i < y; i=i+z)

But what if x > y?

The body of the loop doesn’t get executed, as expected and presumably intended.
What’s your point?

The a good design tip for micro controller programming is DON'T use for or while loops!. As for how to use a fro loop just look in the reference section.

Mark

BS Try displaying a ASCII table without it. If they are not good design, then get rid of them.

So, are we done here?

As for how to use a fro loop just look in the reference section.

I looked. Nothing on fro loops.

So, are we done here?

No

The body of the loop doesn’t get executed, as expected and presumably intended.
What’s your point?

Why?

Point is Reference, for, Coding Tips, is Not True.

Nothing stopping you from writing in BASIC, if that is really what you want.

What I want is a Single for loop to step a servo left or right with No if statements.

steinie44:

for(x=0; x < 10; x=x+2)

How about

for(i=x; i < y; i=i+z)

But what if x > y?

steinie44:

So, are we done here?

No

The body of the loop doesn’t get executed, as expected and presumably intended.
What’s your point?

Why?

Why?
If x > y, i is also > y.

Why anyone would want to cripple C down to the level of BASIC is beyond me.

steinie44:
OK, of so, how do I write an example like: In Basic: for i = x to y step z ? in one loop.
Any of the 3 values can be + or -, x can be > y or < y.

Ok, here’s my attempt:

for (i=x; z>0 ? i<=y : i>=y; i+=z)

steinie44:
What I want is a Single for loop to step a servo left or right with No if statements.

How would you do that in Basic?

What about

int start = 0; // or 180
int end = 180;  // or 0
int step = 5;  // or -5

for (int n = start; n != end; N += step) {
   servo.write(n);
  delay(50);
}

…R