ESP32 / ESP8266 Arduino: Conditional operator

In this tutorial we will analyze how to use the C++ conditional operator, also known as the ternary operator, on the Arduino core running both on the ESP32 and on the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.


Introduction

In this tutorial we will analyze how to use the C++ conditional operator, also known as the ternary operator, on the Arduino core running both on the ESP32 and on the ESP8266.

This operator takes 3 operands and has the following syntax:

Expression1 ? Expression2 : Expression3

The first expression is evaluated and implicitly converted to a Boolean. If it evaluates to true then the second expression is evaluated. If it evaluates to false, then the third expression is evaluated instead.

The final result of the conditional operator is either the result of the second or of the third expression.

Note that this operand behaves pretty much like a if else block, but it leads to a much more compact syntax. It is particularly useful for conditional variable assignment.

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.


The code

We will write our whole code in the Arduino setup function. We will start by opening a serial connection to output the results of our program.

Serial.begin(115200);

The first thing we are going to do is using the conditional operator to conditionally assign a value to a variable accordingly to an expression.

We will first declare an integer which we will use for comparison as the first operand of the conditional operator. We will call this variable cond.

int cond = 10;

Now we will declare a string variable and assign it a value if cond is greater than 5 and another if cond is lesser or equal than 5. We will do this choise using the ternary operator and taking into account the syntax mentioned in the introductory section.

So, the first expression of the ternary operator will be the condition we want to evaluate: cond > 5. Then, after the question mark, we specify the expression that will be evaluated if the previous condition is true. Since we are doing an assignment, we simply put the string value we want to assign.

Now, after a colon, we put the expression that should be evaluated in case the condition is false. Again, since we are assigning a variable, we put the string value we want to assign.

String result = cond > 5 ? "Greater than 5" : "Lesser or equal to 5";

After that, we will print the result. Note that since the condition returns true, the value that should be assigned is the first string, more precisely, “Greater than 5“.

Serial.println(result);

Next we will change the value of the cond variable to 3, so the previous condition we were testing becomes false. Then, we apply the ternary operator with the same operands again and print the result. This time, since the condition is false, the second string should be assigned, more precisely, “Lesser or equal to 5“.

cond = 3;

result = cond > 5 ? "Greater than 5" : "Lesser or equal to 5";
Serial.println(result);

The previous use cases cover one of the most common uses of the conditional operator. Nonetheless, there’s another interesting use case that we are going to analyze. It corresponds to having two variables and conditionally assign a certain value to one or another using the conditional operator.

For this test, our first operand (the condition) will simply be a Boolean variable. We will call this variable condition and start by assigning to it the value false.

bool condition = false;

We will also declare two integers, x and y, and initialize them with the value zero.

int x = 0, y = 0;

After that, we will do the conditional assignment of an arbitrary value to x or y, using the conditional operator. In the code below, since the first operand has the value false, then 10 should be assigned to y, and x should keep the value 0.

(condition ? x : y) = 10;

Serial.println(x);
Serial.println(y);

Finally, we will set the variable condition to true and reset x and y to zero. Then, we will apply the conditional operator again. In this case, should be updated to 10 and y should keep the value 0.

condition = true;
x=0;
y=0;

(condition ? x : y) = 10;

Serial.println(x);
Serial.println(y);

The final source code can be seen below. It contains some additional prints for a better readability.

void setup() {

  Serial.begin(115200);
  Serial.println();

  int cond = 10;

  String result = cond > 5 ? "Greater than 5" : "Lesser or equal to 5";
  Serial.println(result);
  Serial.println("----------------");

  cond = 3;

  result = cond > 5 ? "Greater than 5" : "Lesser or equal to 5";
  Serial.println(result);
  Serial.println("----------------");

  bool condition = false;
  int x = 0, y = 0;

  (condition ? x : y) = 10;

  Serial.print("X: ");
  Serial.println(x);
  Serial.print("Y: ");
  Serial.println(y);
  Serial.println("----------------");

  condition = true;
  x=0;
  y=0;

  (condition ? x : y) = 10;

  Serial.print("X: ");
  Serial.println(x);
  Serial.print("Y: ");
  Serial.println(y);
  Serial.println("----------------");

}

void loop() {}

 

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. Once the procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1.

ESP32 ESP8266 Ternary operator.png

Figure 1 – Output of the program.

As can be seen, the obtained results match the ones we have predicted in the code section. For the first use case, the first sentence was assigned to the variable, since the expression computed to true. After changing the expression to result in false, then the second sentence was assigned.

For the third use case, since the expression was false, then the second variable (y) was assigned and the first one (x) kept its original value. When the changed the expression to true, we obtained the inverse result.

Advertisements
This entry was posted in ESP32, ESP8266 and tagged , , , , , . Bookmark the permalink.

2 Responses to ESP32 / ESP8266 Arduino: Conditional operator

  1. Carter says:

    Thanks for your tutorials – short and to the point, as well as easily understood.

    Liked by 2 people

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s