Reference Language (extended) | Libraries | Comparison
if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. For example, an analog input could be tested and one action taken if the input was less than 500, and another action taken if the input was 500 or greater. The code would look like this:
if (pinFiveInput < 500) { // action A } else { // action B }
else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time:
if (pinFiveInput < 500) { // do Thing A } else if (pinFiveInput >= 1000) { // do Thing B } else { // do Thing C }
You can have an unlimited nuber of such branches. (Another way to express branching, mutually exclusive tests is with the switch case statement.
Coding Note: If you are using if/else, and you want to make sure that some default action is always taken, it is a good idea to end your tests with an else statement set to your desired default behavior.
Corrections, suggestions, and new documentation should be posted to the Forum.
The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.