Arduino – Conditional Execution

Our program sometimes you can continue to run in several ways, we can make these conditional, and the program will execute the appropriate instructions if the conditions are met.

if statement

The “if” statement checks if the condition is true. If true, execute the instruction. Curly parentheses can be omitted after the if condition if only one conditional statement needs to be executed. The semicolon indicates the end of the statement. However, I recommend using curly parentheses anyway because it makes the code more transparent and if you want to add another set of instructions to the block later, you will need it. Be careful to use a comparison operator in the condition. If we accidentally use an single equal sign “=”, it is a value assignment and the condition will always be true. You can also use the “if” statement on your own.

int a = 10;

if(a == 10) 
{
  // statement, if the condition is true
}

else statement

After the “if” statement, you can specify an optional “else” statement that is executed if the expression is false.

int a = 10;

if(a == 10)
{
  // statement, if the condition is true
}
else
{
  // statement, if the condition is false
}

else if statement

Following the “if” statement, you can also specify an optional “else if” statement if you want to examine several different conditions. You can specify any number of “else if” branches, while “if” and “else” can only be one.

int a = 8;

if(a <= 10)
{
  // if the first condition is true
}
else if(a > 10)
{
  // if the second condition is true
}

We can also write the combination “if – else if – else” so that we can examine several conditions in the “if – else if” sections and if none of them is true then the “else” code block will run.

int a = 8;

if(a <= 10)
{
  // if "a" is equal to or less than 10
}
else if(a > 10 && a <= 20)
{
  // if "a" is between 11 and 20
}
else if(a > 20 && a <= 30)
{
  // if "a" is between 21 and 30
}
else
{
  // if none of the conditions are met 
}

switch case

Similar to the “if-else if-else” instructions, the “switch case” is also a control structure. Sometimes you need to compare the same variable to several different values ​​and execute different code depending on the value of the variable.

It compares the value of the variable specified in the “switch” condition with the expression specified after the “case” tag. The “switch” goes from instruction to instruction. The instruction is executed only if it finds a “case” expression that matches the value of the variable specified in the “switch” condition. It then continues to execute the instructions until it reaches the end of the “switch” block or encounters a “break” instruction. If none of the “case” tags match, the “default” tag statement runs.

switch (myVar) 
{
  case label1:
    // statements
    break;
  case label2:
    // statements
    break;
  default:
    // statements
    break;
}

? : conditional operator

The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements. Only one of the expressions on either side of the colon(:) is always evaluated.

(expression1) ? (true_expression) : (false_expression);

First, the “expression1” is evaluated, if its value is true, then the “true_expression” is evaluated and the “false_expression” is ignored. 

(if true) ? (is evaluated) : (not evaluated);

If the “expression1” becomes false, the “true_expression” is ignored and the “false_expression” is evaluated. 

(if false) ? (not evaluated) : (is evaluated);

The result will be the value of “true_expression” or “false_expression,” depending on whether the “expression1” is true or false.


myVariable = (expression1) ? (true_expression) : (false_expression);

The next section is about loops.