That is why the default statement is executed. Serial.println("One of the cases is true") Īs we can see in the output above, none of the cases in the above code is true. The || symbol, also known as the OR operator, is used when we want to execute a task even when one condition is true from all the given conditions.įor example, let’s define two conditions to check the range of a number and print a specific text.
The & symbol, also known as the AND operator, is used only if all the given conditions are true when we execute a task. If we want to check multiple conditions before doing a task, we can also use the & and || operators. We used the delay() function to add some delay in the code.
#Arduino else if serial
The output shows that the text World is printed on the serial monitor, which means the first condition is true and the second is false. We can also add an if statement inside the second if statement in the above code.įor example, let’s create a nested if statement, check the given number, and print a text according to the conditions. Each test will proceed to the next one until a true test is encountered. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time. In the above code, if the first condition is true, the program will check the second condition, and if it is also true, the code inside the second condition will be executed. An else clause (if at all exists) will be executed if the condition in the if statement results in false. We can define the second if statement inside the scope of the first if statement. If we want to check one and it is true, we want to check another condition, The if statement in Arduino defines conditions like doing a task if certain conditions are fulfilled. This tutorial will discuss adding an if statement inside another using the nested if statement in Arduino.