Go Down

### Topic: Equality operators don't work in "for" loops (Read 2137 times)previous topic - next topic

#### crlMidi

##### Jul 16, 2012, 02:42 pm
Hello,

This code loops as you'd expect:
Quote

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:
Quote

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

#### James C4S

#1
##### Jul 16, 2012, 02:45 pm
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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

#### PaulS

#2
##### Jul 16, 2012, 02:45 pm
Quote
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.

Code: [Select]
`for (int i=5; i < 13; i++) {`
This will loop with i equal 5, 6, 7, etc. while i is less than 13.

Code: [Select]
`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.

#### crlMidi

#3
##### Jul 16, 2012, 03:21 pm
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

#### James C4S

#4
##### Jul 16, 2012, 03:28 pm

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."
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

#### AWOL

#5
##### Jul 16, 2012, 03:32 pmLast Edit: Jul 16, 2012, 03:49 pm by AWOL Reason: 1
Quote
Normally, you'd expect the middle bit of "for" loops to mean "until".

There's nothing "normal" about that expectation.

Quote
(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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Grumpy_Mike

#6
##### Jul 16, 2012, 03:39 pm
Quote
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.

Quote
It does need documenting more explicitly for Arduino users

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

#### crlMidi

#7
##### Jul 16, 2012, 04:04 pm
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.

#### AWOL

#8
##### Jul 16, 2012, 04:11 pm
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### wildbill

#9
##### Jul 16, 2012, 04:22 pm
Quote
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.

#### Grumpy_Mike

#10
##### Jul 16, 2012, 04:31 pm
Quote
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.
http://en.wikipedia.org/wiki/C_%28programming_language%29

Quote
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.

#11
##### Jul 16, 2012, 04:39 pm

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.

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++
Code: [Select]
`for(;;)` syntax has been in use for nearly 4 decades itself.  Honestly, there is nothing more common than C/C++ syntax.

Go Up

Please enter a valid email to subscribe