Table of Contents

Program control structures

Control structure

If is a statement that checks the condition and executes the following statements if the condition is true. There are multiple ways how to write down the if statement:

//1st example
if (condition) statement;     
 
//2nd example
if (condition)
statement;                    
 
//3rd example
if (condition) { statement; } 
 
//4th example
if (condition)
{
  statement;
}                             

When both true and false cases of the condition should be viewed, the else part should is added to the if statement in the following ways:

if (condition) {
  statement1;    //executes when the condition is true
}
else {
  statement2;    //executes when the condition is false
}

If more conditions should be viewed, the else if part is added to the if statement:

if (condition1) {
  statement1;    //executes when the condition1 is true
}
else if (condition2) {
  statement2;    //executes when the condition2 is true
}
else {
  statement3;      //executes in all of the rest cases
}

The example when the x variable is compared and in the cases when it is higher than 10, the digitalWrite() method executes.

if (x>10) 
{
  digitalWrite(LEDpin, HIGH)  //statement is executed if the x>10 expression is true
}

Logical operators

Logical operators are widely used together with the condition operator if that is described below.

Comparison operators

There are multiple comparison operators used for comparing variables and values. All of these operators compare the value of the variable on the left to the value of the variable on the right. Comparison operators are following:

Examples:

if (x==y){ //equal
  //statement
}
 
if (x!=y){ //not equal
  //statement
}
 
if (x<y){ //less than
  //statement
}
 
if (x<=y){ //less than or equal
  //statement
}
 
if (x>y){ //greater than
  //statement
}
 
if (x>=y){ //greater than or equal
  //statement
}

Boolean operators

Three Boolean logical operators in the Arduino environment are following:

Examples:

//logical NOT
if (!a) { //the statement inside "if" will execute when the "a" is FALSE
  b = !a; //the reverse logical value of "a" is assigned to the variable "b"
}
 
//logical AND
if (a && b){  //the statement inside "if" will execute when the values both of the "a" and "b" are TRUE
  //statement
}
 
//logical OR
if (a || b){  //the statement inside "if" will execute when at least one of the "a" and "b" values is TRUE
  //statement
}

Switch case statement

Switch statement similar like if statement controls the flow of program. The code inside switch is executed in various conditions. A switch statement compares the values of a variable to the specified values in the case statements. Allowed data types of the variable are int and char. The break keyword exits the switch statement.

Examples:

switch (x) { 
   case 0:  //executes when the value of x is 0
   // statements
   break;   //goes out of the switch statement
 
   case 1:  //executes when the value of x is 1
   // statements
   break;   //goes out of the switch statement
 
   default:  //executes when none of the cases above is true
   // statements
   break;   //goes out of the switch statement
}

Check yourself 1. Which code part is correct?

2. What is the output of the next code part?

int x = 0;
 
    switch(x)
    {
 
      case 1: cout << "One";
 
      case 0: cout << "Two";
 
      case 2: cout << "Hello, world!";
 
    }
 

3. In which cases 'switch structure' should be used?