Arduino Basic Tutorial

if and if else statement in arduino programming

 if and if / else statement:

 if statement in Arduino is used to check if a certain condition is true (”True”, 1) or false (”false”, 0). If the condition is true, the instructions will be executed written inside (that is, inside the opening and closing braces) and you can use if/else to run a set of code if the condition is true the if block will be run, and if it false the else block will be executed.

General syntax of if / else statements in Arduino:

Whether the statements of the if block is executed or the statements of the “else” block, when the last line of that section is reached its jumps to the line immediately after its closing brace to continue from there the execution of the program.

We have said that the “else” block is optional. If we don’t write it, the if statement in Arduino looks like this:

In this case, if the condition were false, the interior of the, if statement would not be executed.

There is also the possibility of including one or more “else if” sections, In this case, the final “else” block is also optional. This construction would have the following syntax (there can be all the “else if” that you want):

It is possible to nested if blocks within each other without any limits (that is, more “if” blocks can be put inside another “if” or “else” block, if so need).

Now that we know the different syntax of the “if” block, let’s see what type of conditions we can define between the parentheses of the if statement in Arduino. The first that we must know is that to write correctly in our sketch these conditions we will need to use one of the so-called operators of comparison, which are as follows.

Comparison operators in if statement in Arduino:

== :

Equality comparison

! = :

Not Equality comparison

> :

greater than  Comparison

> =:
greater than or equal to Comparison


less than Comparison


 less than or equal to Comparison

Example1: Comparison operators in if statement in Arduino programming:


if statement in arduino

In the above code a first “if” appears that checks if there is data in the input buffer of the TTL-UART chip pending to be read. If so (that is, if the value returned by Serial.available () is greater than 0), all instructions inside. Instead, if the condition turns out to be false (that is, if Serial.available () returns 0 and therefore there is no data to read), note that the function “Loop()” does not execute anything.

The moment the condition is true, what we have inside the first “if” is the Serial.parseInt() function that recognizes and extracts everything that is sent through the serial channel (for example, using the “Serial monitor”) a whole number, assigning it to the variable “number”. And this is when another “if” arrives which checks if the value of “number” is equal to 23. If not, then it checks if its value is less than 23. And if it is not yet, there is only one option left: that is greater than 23. When executing this sketch through the “Serial monitor”, which We will see is that every time we send some data to the plate, it will respond to us with any of the three possibilities.

In general, the “if” is capable of comparing decimal numbers as well: in the previous example, if “number” is a variable “float” and we substitute Serial.parseInt() By Serial.parseFloat() we would get the same behavior. In the case of strings, however, keep in mind that you can only use the comparison operators when both strings have been declared as String objects: if they are character arrays, they cannot be compared. In this sense, comparing whether one string is greater or less than another simply means evaluating the strings in alphabetical order character after the character. Emphasize that string comparisons are case-sensitive (that is, the data of type String “hello” is not the same as data of type String “HELLO”).

Example2:Equality operators in if statement in Arduino programming:


if statement in arduino


if statement in arduino

Logical operators in if statement in Arduino:

&& :

Check that both conditions are true (AND operator)

|| :

Check that at least one of two conditions is true (OR operator)

! :

Checks that the condition that precedes is not met (NOT operator)

The “AND” operator forces all the conditions within the parentheses of the “if block” are true: if there is only one that is no longer true, the total set neither will it be. For the “OR” operator, on the other hand, it is enough that, of all the conditions, only one is true so that the total set is also true. The NOT operator changes the state of the condition that follows: if that condition was true becomes false, and vice versa.

Example3:Logical operators in if statement in Arduino programming:


if statement in arduino

As you can see only the “statement 2″ and ” statement 3″. This is because the condition of the first “if” is false: it checks himself if the variable “x” is greater than 10 AND AT THE SAME TIME if it is less than 20. On the other hand, the condition of the second “if” is true: it is checked if the variable “x” is greater than 10 OR less than 20: as one of the conditions is already met –the first–, the total condition is already true regardless of what the rest of the conditions are worth present. The condition of the third “if” is also true: it checks if the variable “x” is greater than 10 and at the same time, thanks to the operator “!”, that it is NOT less than twenty.

Another very useful feature of the “if” is to be able to use the parentheses within a condition to establish the order of comparison of several expressions. Expressions within the values ​​are always compared first. parentheses, or in the innermost if there are several groups of parentheses nested. For example, we could have a condition like this: if (x! = 0 || (y> = 100 && and <= 200)). In this example, we first check what is inside the parentheses, that is, that “y” is greater than or equal to 100 and that it is also less than or the same as 200. That, or that “x” is different from 0. The nesting of conditions can be as complex as you want, but make sure that you are really checking what you want, which sometimes, with very long expressions, it is difficult.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button