🤩 FREE Arduino Crash Course 👇👇
bit.ly/get_Ard...
**Want to learn more? Check out our courses!**
bit.ly/3EMCjCy
**Get your Free Trial of Altium PCB design Software**
www.altium.com...
**Get the code, transcript, challenges, etc for this lesson on our website**
bit.ly/3eD2XU5
**We designed this circuit board for beginners!**
Kit-On-A-Shield: amzn.to/3lfWClU
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: / programmingelectronics...
Twitter: / progelecacademy
Website: www.programmin...
________________________________
Have you been doing some coding and you come across a question mark in the code and you're like, "What is this? What does this mean?" Usually it's followed by a colon. You know, it's sometimes it's like a one-liner and I don't know, sometimes it's just not exactly clear what's going on. Well, this expression with the question mark in the colon is a ternary operator, or conditional operator, same difference. In this lesson, we're gonna talk about exactly what this is, what it looks like, what it does. Basically, it's 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. And in this lesson, you're gonna learn exactly how to use them. Stay tuned. Subscribe to our RU-vid channel to get more videos like this. Before we start, just a big shout out to Altium for sponsoring this video. Huge thanks. You can check the description to get a free trial of the Altium software. So what is going on with these ternary conditional operators? What is this? Well, think of a traditional if statement, right. In a traditional if statement, and that is if else statement, we have a condition, right? If this condition is true, hey, do one thing. If this condition is not true, do something else, right, that's our if else statement. Well, the ternary operator, same deal, it's just formatted a little bit differently and it's all on one line. It's more terse. So with the ternary, you say, "Hey, what's the condition?" Like maybe is A greater than B, or is A like five times B, or whatever expression you wanna have right here, and then you follow it with a question mark, and then you have two values separated by a colon. That's gonna end with a semicolon too. If this expression is true, then what's gonna happen is this A is going to be assigned, right, this A is gonna be returned. If this expression is false, if A is not greater than B, then B is gonna get returned. All right, so that's probably clear as mud so we're gonna look at some concrete examples here, but let's look at line 10 here. I'm just gonna change something here real quick 'cause I don't like the wording exactly. All right, so let's take a look at line 10 here. This puts a little bit of meat on the bones here, but let's say we've got two inputs, right. Here's a previous input and here's a new input, right. So imagine that these are values, right, like maybe this is the number three and this is the number five and we're just checking, hey, is the previous input greater than the new input? Oh, hey, is three greater than five? Or let's say, hey, is six greater than five? Yes, six is greater than five, so what are we gonna do? Hey, previous input is gonna get returned. All right, so previous input is gonna get returned from this ternary operator, conditional operator. Well, what if it's not? What if this is three and new input is like 12, right? Well then the previous input is not greater than the new input, so what's gonna get returned? Well, new input would get returned. So let me do just a little bit of coding here...
CONTINUED...
bit.ly/3eD2XU5
**About Us:**
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.
**We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.**
29 авг 2024