Arduino - Else

Arduino

Guide

Tutorials

Reference

Hardware

Software

Forum

Playground

Exhibition

Hacking

FAQ

Buy

Language | Libraries | Comparison

## if/else

if/else gives you greater control over the flow of your code than the basic if statement, by allowing you to group multiple tests together. For instance, if you wanted to test an analog input, and do one thing if the input was less than 500, and another thing if the input was 500 or greater, you would write that this way:

    if (pinFiveInput < 500)
    {
      // do Thing A
    }
    else
    {
      // do Thing 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.

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

Edit Page | Page History | Printable View | Recent Site Changes