if (condition) {
//Your program
}
We see that the if function takes as parameter a condition which is boolean: either false or true.
We will see an example of a true condition:
if (7>5) {
Serial.println("Your program");//Your program
}
By writing that 7 is greater than 5, the condition is true so the lines of code inside the brackets will be executed. By returning the condition, i.e. 7<5, the condition becomes false and therefore what is inside the brackets will not be executed.
The condition for the if can be hard to write, especially with all the operators that exist. We recommend our course on mathematical functions to have a complete table of all the operators that can be used on arduino!
The else is used when the if condition is false. It is an alternative to the if, if this one is not executed then the else will be.
Here is an example for the else:
if (condition) {
// If the condition is true
}
else {
// If the condition is false
}
Here is an example where the if condition is false so that the program executes the else:
if (7<5) {
Serial.println("if");// program not executed becasue the conditon is false
}
else {
Serial.println("else"); // Program executed
}
As you have seen, the else has no condition, so it will always be executed if the if is false. You may want to make another if to test a second condition. For that there is the “else if”.
The “else if” allows to test a second condition after the first if. The “else if” like the simple if will be executed only if the condition is true. You can put as many “else if” as you want:
if (condition1) {
// program of the if
}
else if (condition2) {
// program of the first else if
}
else if (condition3) {
// the second else if
}
else {
//Code executed only if the conditions of if and elseif are false.
}
Here is an example:
if (4==3) { // false condition
Serial.println("if");// "if" program won't be executed
}
else if (4<2) { // false condition
Serial.println("first else if ");// the first "else if" won't be executed
}
else if (4>2) { // true conditon
Serial.println("second else if ");// the second "else if" will be executed
}
else {
Serial.println("else"); // this code won't be executed
}
Here as we can see, it is the second “else if” that will be executed because the conditions before it are false.
However, if you have a lot of “else if” to put, an alternative is the switch case.
The switch case allows to test several conditions, making a cleaner code instead of using several if or else if. The switch case doesn’t replace in all cases the if and else if.
It uses a variable that contains an integer, a string or a boolean. Each cases is used to differentiate what the program must do if the variable is equal to the condition 1, or 2. If the variable does not correspond to any case, then the code in default will be executed.
Here is an example:
int your_variable=1;
switch (your_variable) {
case 1:
Serial.println("your first condition"); // This case will be executed
break;
case 2:
Serial.println("your second condition"); This case won't be executed
break;
default:
Serial.println("default"); // Code will be executed only if the program didn't entered in any case
break;
}
In this example your variable will be compared to each case to know which code to execute. Here the value of “your_variable” is 1 so the executed code will be case 1.