It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: \$2000
Amount Raised: \$1464

Top Donor: C.C. \$100

X

# Making Decisions with if-else

Created on: 15 November 2014 | Updated on: 1 February 2017

Part 10 of the Arduino Programming Course

Following on from part 9 of the Arduino programming course which covered the if statement, we now look at the if-else construct.

This construct adds more decision making capability to the if statement.

## Using if-else

When using an if statement, the code in the body of the if statement is run only when the if statement evaluates to true. When it evaluates to false, program execution skips the code in the body of the if statement and continues below the body of the if statement.

By adding an else statement, code in the body of the else statement will run only when its corresponding if statement evaluates to false.

The following code shows the syntax of the if-else construct.

```if (conditional expression) {
}
else {
}
```

The code below shows how the functioning of the if statement compares to the functioning of the if-else construct.

```// "if" statement
if (conditional expression) {
// Code placed here only runs if conditional expression is true
}
// Whether the conditional expression evaluates to true or false,
// code placed here will run

// "if-else" construct
if (conditional expression) {
// Body of the "if" statement between { and }
// Works as a normal "if" statement, code placed here will only
// run if the conditional expression evaluates to true
}
else {
// Body of the "else" statement between { and }
// Code placed here will always run if the conditional expression
// from the "if" statement evaluates to false
}
// Code placed below the if-else construct will always run whether
// the conditional expression evaluated to true or false
```

When the conditional expression evaluates to true:

1. Code in the body of the if statement is run.
2. Code in the body of the else statement is not run.

When the conditional expression evaluates to false:

1. Code in the body of the if statement is not run.
2. Code in the body of the else statement is run.

Amazon.com

Amazon.co.uk

## if-else Example Sketch

The sketch below shows an example of using the if-else construct. The LED will switch on if the character 'a' is sent to the Arduino using the serial monitor window. If any other character except 'a' is sent, then the if statement evaluates to false and the code in the else block is run which switches the LED off.

```void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

void loop() {
char rx_byte;

if (Serial.available() > 0) {    // is a character available?
if (rx_byte == 'a') {
// switch the LED on if the character 'a' is received
digitalWrite(13, HIGH);
}
else {
// switch the LED off if any character except 'a' is received
digitalWrite(13, LOW);
}
}
}
```

### Notes on the if-else Sketch Example

#### Nested if-else Construct

The if-else construct in the above example is actually placed inside the body of another if statement. This is called "nesting" and we say that the if-else is nested inside the first if statement.

To make this clearer, this is the if-else construct that is nested:

```if (rx_byte == 'a') {
// switch the LED on if the character 'a' is received
digitalWrite(13, HIGH);
}
else {
// switch the LED off if any character except 'a' is received
digitalWrite(13, LOW);
}
```

The above code is nested inside this if statements body:

```if (Serial.available() > 0) {    // is a character available?
}
```

This video shows the sketch operating.

Can't see the video? View on YouTube →

#### How the Sketch Works

The example sketch is based on the sketch from the previous part of this course. Instead of switching the LED off when only 'b' is sent, the LED will switch off when any character except 'a' is sent.

The character 'a' will switch the LED on as in the previous sketch because the if conditional expression evaluates to true when 'a' is received. Any other character received will cause the conditional expression to evaluate to false. When the expression evaluates to false, the body of the if statement is not run, but the body of the else statement is run instead which turns the LED off.