Equality operators don't work in "for" loops

Hello,

This code loops as you'd expect:

for (int i=5; i < 13; i++) {
Serial.println("i");
Serial.println(i);
//etc.

However, this code doesn't loop, and as there is no error message I spent a bit of time on it:

for (int i=5; i == 12; i++) {
Serial.println("i");
Serial.println(i);
//etc.

The "for" loop doesn't seem to accept an equals sign anywhere in the relational operator (">=" doesn't work either). Has this been documented (it's hard to search for "for" in the forum...)?

Arduino 1.0.1 with mega 2560

The conditional will only cause the for() loop to execute while the conditional is true. If you set the variable to 5 and then say loop while i is equal to 12, the loop won't execute. That's how it is suppose to work.

The "for" loop doesn't seem to accept an equals sign anywhere in the relational operator

It most certainly does. The middle portion of the for statement, though, is a while statement.

for (int i=5; i < 13; i++) {

This will loop with i equal 5, 6, 7, etc. while i is less than 13.

for (int i=5; i == 12; i++) {

This says to loop with i equal 5, 6, 7, etc. while i equals 12. Since i doesn't start at 12, the loop never executes.

Thanks,

It does need documenting more explicitly for Arduino users, then, because this seems to be one of those C++ wierd things that we aren't all familiar with.

Normally, you'd expect the middle bit of "for" loops to mean "until". Various dialects of Pascal and BASIC have separate "for - until" and "while" constructs.

Regards

crlMidi:
It does need documenting more explicitly for Arduino users

This isn't clear enough?
http://arduino.cc/en/Reference/For
"Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends."

Normally, you'd expect the middle bit of "for" loops to mean "until".

There's nothing "normal" about that expectation.

(it's hard to search for "for" in the forum...)?

I wouldn't expect to find documentation on language constructs in the forum, I'd look at the reference pages over at the main site.

Normally, you'd expect the middle bit of "for" loops to mean "until".

I don't think that the C language can be held responsible for you assumptions.

It does need documenting more explicitly for Arduino users

Are you saying that Arduino users are idiots who can't read?

Point taken, except that I use Arduino & don't consider myself a complete idiot.

When using structures that have been in common use in other languages for several decades it should normally be sufficient to look up just the syntax. The documentation and the Arduino Cookbook (700 pages) are very well written; all I was saying is that it would help if the little warnings about the differences between Arduino and pure C++ could be extended to include other languages a user is likely to have encountered.

If you've had the misfortune to write XL VBA macros during the day for your employer, a little reminder that C++ "for" loops don't have the equivalent to "to" in them would make life easier when you switch to Arduino and a different reference book back at home.

If you've had the misfortune to write XL VBA macros during the day for your employer,

I think that comes under the heading of "cruel and unusual", and would be banned in Europe under human rights legislation.

the differences between Arduino and pure C++

There really aren't any. All the Arduino stuff is just C++ libraries provided to make life easier - your code is compiled behind the scenes by the gnu C++ compiler.

When using structures that have been in common use in other languages for several decades

Well C was developed between 1969 and 1973 so there are not many languages that have decades on that.

that I use Arduino & don't consider myself a complete idiot.

So do I, and I would not consider myself a complete idiot either. Let's just say I consider myself a rather incomplete idiot. :slight_smile:

crlMidi:
all I was saying is that it would help if the little warnings about the differences between Arduino and pure C++ could be extended to include other languages a user is likely to have encountered.

That would require a tremendous amount of effort to comprehensively cover the not insubstantial breadth of languages out there that users are likely to have encountered, and largely a waste of effort in my eyes. The existing documentation is more than adequate in describing the usage of the various C++ keywords, and there are numerous tutorials providing even more details in their proper usage.

crlMidi:
When using structures that have been in common use in other languages for several decades it should normally be sufficient to look up just the syntax.

In none of the languages you've specifically mentioned (VBA, Basic, Pascal), is there a for/until structure. Pascal and Basic have a Repeat/Until, but they all use a for structure that is only vaguely similar to C++'s syntax, but substantially more restrictive that iterates through an explicitly declared range of values with no user defined criteria for loop termination.

Also, the C/C++ for(;;) syntax has been in use for nearly 4 decades itself. Honestly, there is nothing more common than C/C++ syntax.