Curly brace
- {}curly braces
Curly braces(also referred to as just “braces” or “curly brackets”)is an important component in C. They are used to identify several different parts. The following listed can make the beginner confused sometimes.
An opening curly brace “{” must always be followed by a closing curly brace “}”.This is often referred to as the braces being balanced. The Arduino IDE 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 logical companion will be highlighted.
Now this function has a bit error, because IDE always takes the braces in the comments as incorrect.
For the beginners and the programmers converting from learning BASIC to C, who are always confused about how to use the curly braces, the curly braces will be used in the “return function”, “endif condition statement”and “loop function”.
Because the curly braces are used in different positions, there is a good programming habit to avoid mistakes:After entering a brace, enter another brace to balance it. Then input enter between the two brace, and inset the statements. In this way, your braces won’t be unbalanced.
Unbalanced braces will result in many mistakes, such as impenetrable compiler errors, which are hard to be found in a program sometimes. Because of its different usages, brace is also an important syntax in programs, and mistaken braces usually affect the meaning of programs greatly.
The main use of curly braces
- Function
void myfunction(datatype argument){ statements(s) }
- Loop
while (boolean expression) { statement(s) } do { statement(s) } while (boolean expression); for (initialisation; termination condition; incrementing expr) { statement(s) }
- Condition statement
if (boolean expression) { statement(s) } else if (boolean expression) { statement(s) } else { statement(s) }