What Are Curly Braces in Arduino Programming?
Curly brackets are syntactic constructs in the Arduino programming language that are written in “{ }” form. Curly braces tell us about starting and ending of certain instructions such as loop and conditions. Curly brackets denote programming language constructs such as functions, conditional statements or any array subscripts. Curly brackets enclosed instructions from a function and conditions for the statements.
Use of Curly Braces in Arduino
In Arduino programming curly braces or important constituents of Arduino sketch. They are the part of two main functions inside Arduino code which are setup() and loop() functions. Curly brackets are not limited to these functions only; they can also be used to define other blocks of code as well.
Use of curly braces is very simple. Whenever there are left curly braces, it will be closed using a right curly brace. Unclosed braces inside Arduino will result in compilation error. Newbies to Arduino or C/C++ programming find it daunting and confusing the use of curly braces in code, but these curly braces work the same way as the ENDIF does in conditional statement and the NEXT statement in for loop and last one RETURN in any function.
Bare Minimum Sketch
Arduino has a very cool and convenient feature to check curly braces inside Arduino sketch. Simply select the starting left curly bracket, the Arduino IDE will highlight the ending or right curly braces.
Best way to demonstrate use of curly braces is to open a bare minimum sketch in Arduino IDE. Here both setup and loop function have curly braces. Click any of the single curly braces and it will highlight the second half.
Now if we remove curly braces from any of the functions, the sketch will not be compiled resulting in Compilation error:
Using Curly Braces in Arduino Programing
Curly braces are widely used in Arduino programming here are list of some common applications in Arduino programming:
- Declaring Variables
- Creating Functions
- Creating Loops
- Making Conditional Statements
Declaring Variables
Normally in C language we initialize a variable using an equal to “=” sign. But Curly braces can also be used to declare a new variable as we did in the below program. Curly braces also prevents Narrowing issue which is implicit conversion for Arithmetic values that includes accuracy loss.
Creating Functions
We can also declare functions using the curly braces. Curly braces are stated after the name of the function followed by a statement and ended by the right curly bracket.
Creating Loops
Curly braces are widely used in Arduino to create different loops such as for, do and while loop. Below program indicates a while loop to do a repetitive task for 200 times.
Below is the basic syntax of declaring loops using curly braces.
For Loop:
To initialize the For loop inside Arduino code we use curly braces.
// any statement(s)
}
For more info about For loop click here.
While Loop:
While loop can be initialized by using curly braces before and after the statement.
// any statement(s)
}
To check how we can use While loop in Arduino click here.
do Loop:
Using the boolean expression do-while loop can be initialized. Before the while boolean expression curly braces are used and a statement is written between them.
// any statement(s)
} while (boolean expression);
To know more about use of do-while loop in Arduino programming click here.
Making Conditional Statements
Conditional statements can also be declared by using curly braces. For example, if-else-if condition, first we specify the condition inside the curly parenthesis the statement or body begin with left curly braces which is ended using a right curly bracket. Inside the curly braces statement which is to be checked is defined along with the instruction that is to be executed will be given.
FAQs
Q: Can I Change Curly Braces Syntax or Positioning?
Yes, syntax of curly braces can be changed but one thing to keep sure is that the bracket must end at some point once started. Changing syntax of curly braces is not recommended as Arduino followed a specific format to write code. It will have no effect on program compilation but when multiple people working on the same project from different backgrounds might find it irritating.
Here both functions will be compiled without giving any error but the correct format which most of the Arduino community follows is used in loop function.
Q: What does the Curly Brackets {} do in Function Definition?
Curly braces in Arduino function begin and end the function block and statements blocks such as in for loop and if conditions. Curly braces are also used in array functions to declare the initial values.
Conclusion
Curly braces are a significant part of Arduino programming. They are used everywhere in Arduino sketches such as declaring a function or writing a for, while loops and declaring a statement. Curly braces are used in a variety of different ways in Arduino; they mark the beginning and ending of certain sets of instructions.