aboutsummaryrefslogtreecommitdiffstats
path: root/source/arduino/else.rst
blob: f278a0d2f9316d75d0a51fb388cf6f9e531f1d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
.. _arduino-else:

if / else
=========

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



Each test will proceed to the next one until a true test is
encountered. When a true test is found, its associated block of
code is run, and the program then skips to the line following the
entire if/else construction. If no test proves to be true, the
default **else** block is executed, if one is present, and sets the
default behavior.



Note that an **else if** block may be used with or without a
terminating **else** block and vice versa. An unlimited number of
such **else if** branches is allowed.



::

    if (pinFiveInput < 500)
    {
      // do Thing A
    }
    else if (pinFiveInput >= 1000)
    {
      // do Thing B
    }
    else
    {
      // do Thing C
    }

Another way to express branching, mutually exclusive tests, is with
the `switch case <http://arduino.cc/en/Reference/SwitchCase>`_
statement.



See also:
---------

`switch case <http://arduino.cc/en/Reference/SwitchCase>`_