Delicious Bookmark this on Delicious      HOME > > ARDUINO LANGUAGE COURSE  t.o.c.

Tour of Comparison Operators

This is one of a collection of pages which, together, attempt to show you "everything" about the Arduino's programming language.

There is a page for you with more information about the project in general, and the way these pages are organized, if you want that.

Please visit my page about power browsing notes sometime.

This page, and the software it references, ©TK Boyd, 1/2010.


You've seen many comparison operators...

if (bToShow>7)...

... uses the ">" ("is greater than") comparison operator. There's also the "<", "less than" comparison operator.

This page is not so much a tutorial as a brief tour of the various operators available to you when programming an Arduino.

There's the "==" comparison... remember: If you mean to compare two things, use TWO equals signs, especially as the compiler won't notice if you only use one... but the code won't do what you expected... it will try to assign what is on the right of the equals sign to what is on the left. Thus "bTmp=5" will do an assignment (if the variable has been declared)... which is very different from asking "Is five stored in bTmp?", which you ask with "bTmp==5"... TWO equals signs.

There's "!=" for NOT equal, i.e., the line....

if (4!=5) {PrintAnswer}

... will always do the "PrintAnswer" function, as it is TRUE that 4 does not equal 5. (You would, of course, never write something exactly like the above. You would have a variable in place of at least one of the two numbers.)

There are also operators for "greater than or equal to..." (>=) and "less than or equal to" (<=). I tend to avoid these. As long as you are using a whole-numbers-only data type, it is just as valid to say....

if (bTmp>7)

... as it is to say...

if (bTmp>=8)

... and I suspect that the first one executes just a tiny bit more quickly, uses just a tiny bit less memory. Will either ever "matter"? Probably not.... but there is a principle of elegance involved.

... and you should also consider the following:

What if the line above is needed as part of the "count up/ count down" program we developed a few tutorials ago. The line was....

if (bToShow>7){bToShow=0;};

Now, we SHOULD have been able to say....

if (bToShow==8){bToShow=0;};

... as we should never arrive at that point in the program with any number other than 0-8 in bToShow, and when it held 8, we needed it reset to zero. But what SHOULD happen in a program is not always what DOES happen. In the scenario given, it is just a little bit safer to use the former statement. The program will then reset bToShow to zero whenever it holds more that 7, be that 8, or 9, 10, 11... etc.

I told you this discourse was short!

Perhaps I should just mention that some of the bitwise operators can be used to construct conditional tests, but that's covered elsewhere.

   Search this site or the web      powered by FreeFind

Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

SPELL your search term properly. When I review search logs, it is amazing how many people ask the engine to search for something meaningless.

Why does this site cause a script to run? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out their site. And if there are Google ads on the page, they are run with scripts, too.

Click here to return to Arduino COURSE table of contents.
Click here to go to the author's home page.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Here is how you can contact this page's editor. This page, and the software it references, ©TK Boyd, 1/2010.

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at