Тёмный
No video :(

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

Programming Electronics Academy
Подписаться 231 тыс.
Просмотров 12 тыс.
50% 1

🤩 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

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 70   
@robotcantina8957
@robotcantina8957 2 года назад
This is a slick way to compress the code, especially the one line variant.
@programmingelectronics
@programmingelectronics 2 года назад
I agree! Thanks for watching!
@RobiBue
@RobiBue 2 года назад
@@programmingelectronics I agree too :) albeit I’d have compressed it even further… like: DigitalWrite(ledPin, (input > 100) ? HIGH : LOW); great video and explanation with visuals!
@RobiBue
@RobiBue 2 года назад
P.s. just noticed someone else had the same idea …
@bibel2k
@bibel2k 2 года назад
I never saw this kind of coding! looks amazing and easy to understand! ‏thank you You got a thumbs-up and another subscriber.
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for the sub!
@al-gaverlasaad5459
@al-gaverlasaad5459 Год назад
The way you explain the code so mazing, super clear and easy to understand. New subscriber here.
@programmingelectronics
@programmingelectronics Год назад
Awesome, thank you!
@johnsun2416
@johnsun2416 7 месяцев назад
Thanks for teaching, this is much better than if/else, where I don't have to deal with those pesky brackets.
@programmingelectronics
@programmingelectronics 7 месяцев назад
I like it a lot too! Thanks for the note
@irgski
@irgski Год назад
Very clear description! Thank you Sir!
@programmingelectronics
@programmingelectronics Год назад
You are welcome! Thanks so much for the note!
@wilmercb
@wilmercb 2 года назад
Great explanation! I hope you’re the one soon showing tutorials integrating Arduino and iOS/Swift code 😉
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for the recommendation on that Rikeõ! Is there any specific application you have in mind that you would like demoed?
@wilmercb
@wilmercb 2 года назад
@@programmingelectronics Well, I saw your Hydroponic garden video (IoT) and I’ve seeing many BLE/Wifi projects/kits, but all of them always use a pre-made iOS app (even in Android) or a limited cloud interfae, so I was thinking a project that incorporate a custom iOS app design with a BLE or WiFi modules to enable HomeKit. Like Philips HueLights …etc Hope I’m making sense.
@programmingelectronics
@programmingelectronics 2 года назад
@@wilmercb Thanks! Yes that makes sense!
@crisselectronicprojects8408
@crisselectronicprojects8408 2 года назад
Great explanation!
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for a ton for watching!
@warrenscorner
@warrenscorner 2 года назад
I never heard of that operator before but I like it. The less typing I have to do the better. Thank you
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for watching Warren! I see the ternary pop up in code now and again, so it's nice to recognize it.
@yashaswikulshreshtha1588
@yashaswikulshreshtha1588 2 года назад
@@programmingelectronics Hey love your video. Can you please make a video for pointers and unions etc?
@yashaswikulshreshtha1588
@yashaswikulshreshtha1588 2 года назад
@@programmingelectronics I think A lot of people like me are learning programming using Arduino because it's basically C. It would be awesome if you can make videos on programming concepts like pointers unions, file handling etc
@qcnck2776
@qcnck2776 2 года назад
Nice explanation. Though I find the longer IF statement easier to read.
@programmingelectronics
@programmingelectronics 2 года назад
Thanks! I kind of go back on forth on which I prefer...
@leitto-corleone
@leitto-corleone 2 года назад
this operator only works if the variable is only fix with two possible value, beside that, if else or even switch case statement are the way to go
@user-fr3hy9uh6y
@user-fr3hy9uh6y 2 года назад
Good explanation. Ever since C was invented, yes I'm that old, there has been a contest to see who can write the most complex program in a single line. Readability and maintainability is my mantra. If you show it to another programmer is it more readable? Yes it did save 4 lines of source code but source code is free, add a whole bunch of comments and see how much your run time code grows. You didn't show how much ram you saved. I'm guessing a good compiler would make them the same.
@programmingelectronics
@programmingelectronics 2 года назад
Great points Cliff!
@AdeKingProductions
@AdeKingProductions 2 года назад
a >b ? thisShows : thatShows. The repeat of the condition in the statement can confuse some in its application. Great explanation though.
@jumadhaheri
@jumadhaheri 2 года назад
Happy New year thanks
@programmingelectronics
@programmingelectronics 2 года назад
Thanks! Happy New Year!
@vinothn4228
@vinothn4228 2 года назад
Pure clearance man👌...
@michaelpopov1548
@michaelpopov1548 Год назад
Simply great explanation!
@programmingelectronics
@programmingelectronics Год назад
Thank you!
@J_Creative
@J_Creative Год назад
Nice,ang clear thank you
@programmingelectronics
@programmingelectronics Год назад
Thanks!
@patrickmclaughlin6013
@patrickmclaughlin6013 2 года назад
have to try, I think digitalWrite(ledPin, ((input>100) ? HIGH : LOW))); might work too?
@programmingelectronics
@programmingelectronics 2 года назад
Nice, just checked - it works! Thanks Patrick! (small side note: one less parenthesis on the right.)
@henrybest4057
@henrybest4057 2 года назад
@@programmingelectronics Also removes a variable (state) so will probably run a little faster, if that's important.
@LilGh02t
@LilGh02t 2 года назад
the below statement also works for this case, is simpler, without the ternary operator. digitalWrite(ledPin, input>100);
@programmingelectronics
@programmingelectronics 2 года назад
@@LilGh02t Nice!
@explorerpragun431
@explorerpragun431 2 года назад
Thanks dude
@programmingelectronics
@programmingelectronics 2 года назад
You bet! I hope you found it useful!
@umeshdange3844
@umeshdange3844 2 года назад
Best videos thanks sir
@rehnmaak
@rehnmaak 2 года назад
You missed the whole point with ternary operators. The real benefit of using the ternary operator is to use it as an argument to a function call for instance in your call to DigitalWrite to determine if it is going to be high or low. This way you don't have to declare an additional variable state just to save the result that goes into the DigitalWrite call.
@xfran70
@xfran70 2 года назад
And what about " digitalWrite (ledPin, (input > 100)) " ?
@programmingelectronics
@programmingelectronics 2 года назад
Nice!
@TinLethax
@TinLethax 2 года назад
Ternary can make your C code looks small, but it depends on some compiler. If it smart enough, compiler can optimize it but some compiler can't .I use ternary in bare metal C compile with SDCC (target is stm8 core). In some situation the normal if else costs less resource than the ternary. Just want to point out for someone playing with weird, not popular architecture/compiler.
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for pointing this out!!
@avrphreak9121
@avrphreak9121 2 года назад
Interesting! Keep it up, i subscribed !
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for the sub!
@Всемирная_Робототехника
С новым годом!
@programmingelectronics
@programmingelectronics 2 года назад
Thanks!
@electronics8627
@electronics8627 2 года назад
Amazing
@programmingelectronics
@programmingelectronics 2 года назад
Thank you! I hope you found it helpful!
@joshuapitong899
@joshuapitong899 2 года назад
Thank you so much.❤
@programmingelectronics
@programmingelectronics 2 года назад
Thanks for the note!!
@OffGridOverLander
@OffGridOverLander 2 года назад
I find I’m using if/else to check a range and if in certain range turn on or off several functions or relays. Does the ternary work to do this? Something like if a > b turn on c, d, e, and turn off f, g, h
@programmingelectronics
@programmingelectronics 2 года назад
Great question! I think you could make a ternary work for this, but I believe the output is limited a single line of code. For example, in the video, we use digitalWrite() as one of the "options", I don't think you can have more than a single line of code to execute there... (but I could be wrong...) That being said, if you had a flag variable as the output of the ternary then that flag could control flow for several other functions perhaps.
@OffGridOverLander
@OffGridOverLander 2 года назад
@@programmingelectronics I’ll try that on one of the projects I’m working on now. If it works good as just a redirect for a function then I’ll use it on the two larger projects where they are almost nothing but IF statements. Although I’m not sure if I completely grasp the idea of functions yet, I’m at least understanding enough that I’m willing to try. For those curious why so many IFs, I’m making two controllers. One is based on exhaust gas temperatures to “map” how much water the engine gets to keep it from getting too hot. The other is the same idea for propane based on boost pressure since it is a mechanical fuel diesel to add power. Thanks! 👍
@DrRusty5
@DrRusty5 2 года назад
This really sorts of the mess of brackets required with if/else statements.
@programmingelectronics
@programmingelectronics 2 года назад
Totally agree!
@DrRusty5
@DrRusty5 2 года назад
@@programmingelectronics I've just cleaned up some code, in part making multiple "if" statements within a State Machine ternary operators. There have been a few odd errors come up but working through them.
@rodrilea1
@rodrilea1 2 года назад
Thanks this makes sense. How does it work if you introduce delays between states?
@DD-jj2tc
@DD-jj2tc 2 года назад
Is this in the arduino reference
@programmingelectronics
@programmingelectronics 2 года назад
I do not believe it is listed on the Arduino reference page.
@Ahmedhkad
@Ahmedhkad Год назад
I would writing digitalWrite(ledpin,(input>100) ? HIGH : LOW);
@TheJimtanker
@TheJimtanker 2 года назад
If you don't have a Kit-On-A-Shield, then you're not one of the cool kids and probably sit at the front of the bus.
@programmingelectronics
@programmingelectronics 2 года назад
Thanks Jim!
@TheJimtanker
@TheJimtanker 2 года назад
@@programmingelectronics I love my KOAS and still use it years later, especially when I'm showing other people what Arduinos can do.
@catchulater7483
@catchulater7483 Год назад
I QUIT!🤯 I have figured it out! I just absolutely don’t have the intelligence to wipe my a** let alone program whats left of this shotgun blasted uno!🤣 Would you please (now e-begging) do a video for morons on a wireless relay controlling an Arduino uno controlling a MD20A changing the Cytron Motor library where pin 2 and pin 3 (PWM DIR) are changed to pin 3 (PWM) and pin 4 (DIR). Now using pin 2 as an input. It is just a plain Jane 12vdc brushed motor. Go dir a at 255 stop a sec then go dir b at -255 and stop. I have tried everything I have knowledge to do. I got the motor running but just cant get the keyFob to operate anything but a bad attitude! Do I need a shield? (i know at this point the breadboard deff needs one😂🤣😂🤣) seriously tho. Im at my wits end. The message boards are so complicated i just cant even use em. I guess this kinda thing is for folks w/o learning disabilities. Wish I could post this sketch I have. (a true abortion) you would be thoroughly amazed it compiled. (I WAS)
Далее
Using Arrays with For Loops
17:24
Просмотров 31 тыс.
Hacking a weird TV censoring device
20:59
Просмотров 3 млн
Haaland Showed Me How It's Done.. 😜
00:11
Просмотров 2,8 млн
How to Organize Code
14:37
Просмотров 62 тыс.
Using Serial.parseInt() with Arduino
15:39
Просмотров 47 тыс.
Harder Drive: Hard drives we didn't want or need
36:47
Where Does Bad Code Come From?
42:21
Просмотров 191 тыс.
Explaining RISC-V: An x86 & ARM Alternative
14:24
Просмотров 448 тыс.