for loop problem?

How come this loops normally:

for a = 0; a <=20; a++){

}

but this doesn’t, (falls out of the loop after one pass)

for (a = 20; a <=0; a–){

}

Oooops, wrong forum, sorry…

a is defined as an int
second example also fails using --a

How come this loops normally:

for a = 0; a <=20; a++){

It doesn’t compile, so how can it work?

Look carefully at the condition in your second loop.

Discovered that in this code:

void loop() {
int a = 0;
int b = 0;

for (a = 0; a <= 20; a++) {
TPA_HI;
TPA_LO;
}

for (b = 20; b <= 0; b–) {
TPB_HI;
TPB_LO;
}
}

The b loop fails to operate. I guess the third parameter must increment. I thought C++ allowed more flexibility in the third parameter. Would be nice if the compiler flagged this!

Would be helpful to know what "TPA_HI" and "TPA_LO" and "TPB_HI" and "TPB_LO" actually are.

for (b = 20; b <= 0; b--) {already answered in your near-identical post.

Hint: it’s a while loop, in a fur coat

You start the loop by setting b = 20, the loop executes as long as b <= 0. How many iterations will that be?

Hint: It’s a number less than 1…

Regards,
Ray L.

If you set something to 20 exactly how long do expect that it will remain less than or equal to 0?

Steve

Learning these lessons the hard way. Oh well, I’ll just keep my head down and carry on.

I still think the compiler might have scolded me. It’s pretty good at finding other errors.

As an aside, unless you are using a and b outside of the loop, and want them to have the values that the loop left in them, you should declare them in the for() statement, eg:

for (int a=0;a<20;a++)

Thus, the variable a will have a scope limited to the for loop itself, rather than the whole loop() function.

or, if the value will always be positive and less than 256, declare as a byte to save a bit of flash.

RWH10:
Learning these lessons the hard way. Oh well, I’ll just keep my head down and carry on.

I still think the compiler might have scolded me. It’s pretty good at finding other errors.

The code you wrote is perfectly valid, syntactically. Why should the compiler “scold” you? The compiler will do what you tell it to, even if what you tell it to do is silly, or pointless. You cannot expect it to figure out your intent.

Regards,
Ray L.

I've merged your cross posts @RWH10.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

RWH10:
Discovered that in this code:

void loop() {
int a = 0;
int b = 0;

for (a = 0; a <= 20; a++) {
TPA_HI;
TPA_LO;
}

for (b = 20; b <= 0; b–) {
TPB_HI;
TPB_LO;
}
}

The b loop fails to operate. I guess the third parameter must increment. I thought C++ allowed more flexibility in the third parameter. Would be nice if the compiler flagged this!

No, you are failing to read the code to yourself: “for b from 20 while b is less than or equal to zero…”. The loop condition fails immediately since b is not less than or equal to zero, its 20.

If you loop down the condition must use >= or >. The loop stops when the condition fails