aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/arduino/braces.rst
blob: 38018fe7e73942b9fdc1c9cd264135bc384e0174 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
.. highlight:: cpp

.. _arduino-braces:

Curly Braces ({})
=================

.. contents:: Contents
   :local:

Introduction
------------

Curly braces (also referred to as just "braces" or as "curly
brackets") are a major part of the C and C++ programming
languages. They are used in several different constructs, outlined
below, and this can sometimes be confusing for beginners.

An opening curly brace, ``{`` must always be followed by a closing
curly brace ``}``. This is a condition that is often referred to as
the braces being *balanced*. The Maple IDE (integrated development
environment) includes a convenient feature to check the balance of
curly braces. Just select a brace, or even click the insertion point
immediately following a brace, and its companion will be highlighted\
[#fbug]_\ .

Beginning programmers, and programmers coming to C++ from languages
without braces, often find using them confusing or daunting.

Because the use of the curly brace is so varied, it is good
programming practice to type the closing brace immediately after
typing the opening brace when inserting a construct which requires
curly braces. Then insert some blank lines between your braces and
begin inserting statements. Your braces, and your attitude, will never
become unbalanced.

Unbalanced braces can often lead to cryptic, impenetrable compiler
errors that can sometimes be hard to track down in a large program.
Because of their varied usages, braces are also incredibly important
to the syntax of a program and moving a brace one or two lines will
usually dramatically affect the meaning of a program.

The main uses of curly braces
-----------------------------

**Functions**::

      // a function body needs braces around it
      void myfunction(datatype argument) {
        // ... function body goes in here ...
      }

**Loops** (see the :ref:`while <arduino-while>`\ , :ref:`for
<arduino-for>`\ , and :ref:`do/while <arduino-dowhile>` loop reference
pages for more information)::

      // you should put braces around the body of a loop:

      while (boolean expression) {
         // code inside the loop goes here
      }
    
      for (initialisation; termination condition; incrementing expr) {
         // code inside the loop goes here
      } 

      do {
         // code inside the loop goes here
      } while (boolean expression);


**Conditional statements** (see the :ref:`if statement <arduino-if>`
reference page for more information)::

      // you should put braces around the body of an "if", "else if",
      // or "else":

      if (boolean expression) {
          // code inside the "if"
      }
      else if (boolean expression) {
          // code inside the "else if"
      } 
      else {
          // code inside the "else"
      }

.. rubric:: Footnotes

.. TODO remove this once IDE 0.1.0 released

.. [#fbug] At present this feature is slightly buggy as the IDE will
   often find (incorrectly) a brace in text that has been commented
   out.


.. include:: cc-attribution.txt