Pages: [1]   Go Down
Author Topic: Equality operators don't work in "for" loops  (Read 721 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for (int i=5; i < 13; i++) {
This will loop with i equal 5, 6, 7, etc. while i is less than 13.

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 63
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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."
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 16, 2012, 08:49:31 am by AWOL » Logged

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

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 508
Posts: 31405
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3407
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 508
Posts: 31405
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: