aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/arduino/goto.rst
blob: 1fcceb7dc905b171f1ae8aa7194082b8428ec61d (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
.. _arduino-goto:

goto
====

Transfers program flow to a labeled point in the program



Syntax
------

label:



goto label; // sends program flow to the label



Tip
---

The use of *goto* is discouraged in C programming, and some authors
of C programming books claim that the *goto* statement is never
necessary, but used judiciously, it can simplify certain programs.
The reason that many programmers frown upon the use of *goto* is
that with the unrestrained use of *goto* statements, it is easy to
create a program with undefined program flow, which can never be
debugged.



With that said, there are instances where a goto statement can come
in handy, and simplify coding. One of these situations is to break
out of deeply nested *for* loops, or *if* logic blocks, on a
certain condition.



Example
-------

::

    for(byte r = 0; r < 255; r++){
        for(byte g = 255; g > -1; g--){
            for(byte b = 0; b < 255; b++){
                if (analogRead(0) > 250){ goto bailout;}
                // more statements ... 
            }
        }
    }
    bailout: