Control strutures can control what code is executed. This allows the run different code depending on multiple factors like loops and conditions. There are several statements that can be used.
For example, depending on the value of some sensors, it is expected to have a different behaviour. An example would be to turn on a different leds depending on the distance measured by a HR-SR04 ultra sonic distance sensor.
The if statement checks for a condition and executes the following statement or set of statements if the condition is true. The syntax is the following:
The conditions being evaluated inside the parentheses require the use of one or more operators shown below.
Some example of valid usages of the if are.
More information can be read in: https://www.arduino.cc/reference/en/language/structure/control-structure/if/
The if…else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped. An else clause (if at all exists) will be executed if the condition in the if statement results in false. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.
Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior.
Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches are allowed.
Syntax
An example how if/else blocks can be used.
More information can be read in: https://www.arduino.cc/reference/en/language/structure/control-structure/else/