What is the ? code!? Learn about the ternary operator!
Have you come across a “?” in your code, and you’re wondering “What is this”?
Well, this expression is called a ternary operator, or conditional operator. In this lesson, we’re gonna talk about exactly what this is, what it looks like and what it does.
Ternary Operators
Basically, a ternary operator is like a condensed if else statement. They can be really handy to use. It makes the code just concise and easy to read in the right situation.
if else statement
So let’s dive in. What is this? Well, think of a traditional if else statement. In a traditional if else statement, we have a condition. If this condition is true, the code executes one thing. If this condition is not true, the code executes something else. Well, in the case of the ternary operator, it’s the same deal, just formatted differently, and all on one line. So with the ternary, you say, “Hey, what’s the condition?”. Perhaps A greater than B, or A is five times B. whatever it may be, you’re going to specify the variable followed by a question mark followed by two values separated by a colon.
Let’s look at some concrete examples.
THE CODE
All right, so let’s talk through this code we have here. Basically, I’ve got a pin number that I’ve defined, pin number six. I’ve got a long that I’ve defined called input. And then I’ve got a variable that is a Boolean, so that could be either true or false; high or low. I’ve called it state and I said equal to low starting out.Then in the setup, we’re initializing serial communication and setting the mode of that LED pin as an output.Down in the loop on line 21, we’re checking to see if there’s an input available in the serial buffer. Using the serial monitor we can see if we enter a value, let’s say 12 and I hit send, that value is going to the serial buffer and serial available will return the number of bytes that are in the buffer. So if there are no bytes in the buffer, it returns to zero, or false. And so while false, the if else statement wouldn’t run, but if it’s true, then the if statement runs. And if the statement is true and something is available, what we use is the parseInt function. It’s going to look in the serial buffer for the first integer and then it will return that value as a long. And then we just print that out.
Condensing an if else statement
So here we’ve got our standard run-of-the-mill if else statement here.
And we’ve got a condition in this if statement . It’s saying, hey, is the input greater than 100? If it is, then set the state equal to high. Otherwise, if it’s not greater than 100, then we’re gonna set state equal to low.Then on this last line, we just write, digital write to that LED pin. So this is going to be either our high or low. If the input is greater than 100, it’s going to be high. If it’s less than 100, it’s gonna be low. So the LED’s either gonna be on or off based on what we put in to the serial monitor.
condensing into a ternary operator
So let’s turn this into a ternary operator.
So this code right here on line 27 is the exact same thing. It’s gonna have the exact same result as our previous if else statement. Instead of taking up so many lines of code, it’s just one line of code.
First we have our variable called state and we’re setting it equal to the output of this ternary operator. And soo if this condition is true, the code does the next thing. So what’s gonna happen? Well, this value high is going to be returned and saved into state, right? So this is like setting state equal to high. If this is not true, then low, this value low is going to be saved into state.
So we can get rid of our if else statement and our ternary operator with do the exact same thing. Thanks for checking out this lesson and feel free to watch the video for a more in-depth look into this topic!
Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?