What is Syntax??
The syntax is nothing but rules that define how the structure of language will be. For more clearance let’s take an example.
When we write some
lines in a paragraph we use a full stop or period (.), which means the end of the sentence. Similarly, we have comma (,) to separate words in
sentences & inverted commas (“ “ ) to indicate the start and end of speech or
quotes of a person.
Same here in Arduino programming we have semi-colon, comments, curly braces, etc. So the next question will be.
Why do we use Syntax??
The syntax is very important in programming so the reason for
using this syntax is to help and understand our compiler what we have written
in the code.
The compiler is just like your angry English teacher who loves
to put red lines on the mistake you have done on test paper …… (Just kidding).
The compiler also does the same things, it gives you an alert and shows the mistake you made while writing code with highlighted color on that line describing what type of error and what is the cause of occurrence at the bottom window. The window at the bottom also shows messages of success such as done compiling and done uploading.
Here is some important syntax so lets us see them one by one
1) #define : -
#define helps to
give a name to a constant value. Constant values mean never changing values i.e. fix
values. It does not take up any memory space on the chip.
Syntax - #define constant name value
Example - #define ledpin 5
Now the compiler will replace value 5 with ledpin at the time of
compiling
digitalWrite(ledpin , HIGH)
This means Arduino pin 5 which is represented by ledpin is
made HIGH i.e. LED connected to pin 5 will glow.
NOTE – Do not use semi-colon after #define statement.
2) // (single line comments)
Comments are nothing but notes which helps you or
someone else to understand what that line of code means or what that line of
code does. Suppose in the future someone else is handling that project which you
were working on before, the comments help the programmer to know and understand what
this line is meant.
These comments are ignored by the compiler means whatever you have written inside comments, the compiler will just ignore it and compile the rest of the code.
3) /* */ (multiline comments or block comments)
Multi-line comments are similar to single-line comments except the single line is used for one line of code on which we want to comment, while in multiline we can comment on more than one line.
4) ; ( Semi-colon)
The semi-colon is used at the end of the lines in a code except
for the functions ()
For example, we use full-stop or period at the end of a sentence
similarly in programming we use semicolons; which tell the compiler that it's the end
of the line. As we discuss earlier semi-colon is a very important thing in Arduino programming.
Each and every line of code will have a semicolon; except for the functions()
What are functions () that we will discuss in
upcoming lessons?
5) { } Curly braces –
Curly braces { } are used with conditional statements, functions, and loops in Arduino programming. Curly braces are an important syntax of programming, every opening curly brace must have a closing curly brace. If you put the pointer on one of the braces then it will be highlighting another brace corresponding to it. It is mainly done because people sometimes often forgot one of the braces. Whatever we write logic and code should be inside these curly braces.
6) # include: -
#include syntax is used to include outside external libraries in your Arduino sketch. As # define, # include does not require a semi-colon at the end of its statement.
Syntax - # include <file.h>
# include “file.h”
<file.h> syntax is used when your library file is
stored in the default library folder. When the compiler searches for a file in the default
libraries path whereas “file.h” is used when your library file is not in the default folder, it is stored somewhere else, you can access it by putting the address of the file in between double quotes.
Conclusion
-
Today
we learn about what is Syntax in programming, Why to use Syntax, and Arduino programming Syntax for Arduino.
"I hope you find this lesson on Arduino very helpful to you and understand the different syntax used in Arduino. In the upcoming lesson, we will learn about what is a void loop and void setup till then bye. See you all in my next chapter of Arduino lessons."