Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

C++

Blocks


Code Block
languagecpp
/*
   multi-line
   comment
*/

Code Block
languagecpp
// single line comment



Image RemovedImage Added

Image Removed

Image Added

Statements

  • Statements are compiled into an ‘executable’ of 1s and 0s, which are run on the Arduino.

  • Statements end with a ;

C++

Blocks

Code Block
languagecpp
c = a+b; // c is assigned the sum of a, b

Image Removed

Image Added

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


Code Block
languagecpp
int a; // declaration

int a = 0; // declaration and initialization


Image RemovedImage Added

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

Code Block
languagecpp
a = 10; // assigning 10 to a

Image RemovedImage Added

A variable value is used when it appears in a comparison or on the right-hand side of an expression/assignment.

C++

Blocks

Code Block
languagecpp
(a == 1) // checking if a is equal to 1

Image RemovedImage Added

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

Code Block
languagecpp
b = a + 10; // variable b is assigned the value of variable a plus 10.

Image RemovedImage Added

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

Code Block
languagecpp
if(a>0){
    c = a+b;
//executed when a>0
}
 
else{
    c = a-b;
//executed when a<0 or a==0
}
Image RemovedImage Added

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

Code Block
languagecpp
while(a>0){
    a = a-b;
/* executed over and over until a<0 or a==0 */
}

Image RemovedImage Added

To Do : Read up about for loopdo-while loop

...

C++

Blocks

Code Block
languagecpp
{
  digitalWrite(11, HIGH);
}



Image RemovedImage Added

Setup() and loop() functions

...