...
C++ | Blocks | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Statements
Statements are compiled into an ‘executable’ of 1s and 0s, which are run on the Arduino.
Statements end with a ;
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
Variables
Variables are containers to store intermediate data
In C language, a variable needs to have a type, which specifies the nature and the size of data it holds
Some of the standard data types are
int : 16-bit integers in the range -32,768 to 32,767
unsigned int : 16-bit positive integers (incl 0), 0 to 65535
char : 8-bit integers from -128 to 127. Characters such as ‘0’, ‘A’, ‘a’, etc. are represented using ASCII (48, 65, 97 respectively - read up more!). BYTE is the same as unsigned char
float : for floating point numbers such as 2.25, -5.875 (32-bits)
Types can also be user-defined (through classes)
Variables can be declared with or without initialization
C++ | Blocks | |||||
---|---|---|---|---|---|---|
| Note: Creating a variable in Blocks declares it and initializes it to 0 |
A variable appearing on the left-hand side of an assignment statement assigns a value to it.
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
A variable value is used when it appears in a comparison or on the right-hand side of an expression/assignment.
C++ | Blocks | |||||
---|---|---|---|---|---|---|
| ||||||
The comparison above returns a 'true' or 'false', which is usually used for selection or iteration. Read up more about comparison operators! Important : In C++, '==' is a comparison operator, '=' is the assignment operator. Using one instead of the other will not cause a syntax error, but will cause incorrect program functionality. |
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
Selection
A program that executes all the statements in a sequence is probably not very interesting.
Sometimes, a part of a program must be executed based on a certain condition (selection).
Given below is an example of an if-else statement. The {} is optional if there is only one line being executed conditionally for if or else.
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
To Do : Read up about switch-case
...
Sometimes, a part of a program must be executed repeatedly until a certain condition is satisfied (iteration / loop)
Given below is an example for an while loop.
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
To Do : Read up about for loop, do-while loop
...
C++ | Blocks | |||||
---|---|---|---|---|---|---|
|
Setup() and loop() functions
...