Decision making

In the Arduino programming language, we make decisions with the if statement. The if statement will check if a condition is true and if so will execute the block of code within the curly brackets.

The following shows the syntax for the if statement:

if (condition) {
  // Code to execute
}

We can use an else statement after the if statement to execute a block of code if the condition is not true.

The following shows the syntax for the if/else statement:

if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}

The condition, in the if statement, can be any Boolean value or an operation that returns a Boolean result. You will find that the majority of the if statements in your ...

Get Mastering Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.