Загрузка страницы

What is the ? code!? Learn about the ternary operator!

🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills

***Want to learn more? Check out our courses!***
https://bit.ly/3EMCjCy

***Get your Free Trial of Altium PCB design Software***
https://www.altium.com/yt/programmingelectronicsacademy

***Get the code, transcript, challenges, etc for this lesson on our website***
https://bit.ly/3eD2XU5

***We designed this circuit board for beginners!***
Kit-On-A-Shield: https://amzn.to/3lfWClU

FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: https://www.facebook.com/ProgrammingElectronicsAcademy/
Twitter: https://twitter.com/ProgElecAcademy
Website: https://www.programmingelectronics.com/
________________________________

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 YouTube 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...
https://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.***

Видео What is the ? code!? Learn about the ternary operator! канала Programming Electronics Academy
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
31 декабря 2021 г. 20:16:51
00:12:30
Яндекс.Метрика