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

  Рет қаралды 12,333

Programming Electronics Academy

Programming Electronics Academy

Күн бұрын

🤩 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 KZbin 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.**

Пікірлер: 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!
@irgski
@irgski Жыл бұрын
Very clear description! Thank you Sir!
@programmingelectronics
@programmingelectronics Жыл бұрын
You are welcome! Thanks so much for the note!
@johnsun2416
@johnsun2416 8 ай бұрын
Thanks for teaching, this is much better than if/else, where I don't have to deal with those pesky brackets.
@programmingelectronics
@programmingelectronics 8 ай бұрын
I like it a lot too! Thanks 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!
@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
@crisselectronicprojects8408
@crisselectronicprojects8408 2 жыл бұрын
Great explanation!
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thanks for a ton for watching!
@jumadhaheri
@jumadhaheri 2 жыл бұрын
Happy New year thanks
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thanks! Happy New Year!
@vinothn4228
@vinothn4228 2 жыл бұрын
Pure clearance man👌...
@michaelpopov1548
@michaelpopov1548 2 жыл бұрын
Simply great explanation!
@programmingelectronics
@programmingelectronics Жыл бұрын
Thank you!
@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!
@explorerpragun431
@explorerpragun431 2 жыл бұрын
Thanks dude
@programmingelectronics
@programmingelectronics 2 жыл бұрын
You bet! I hope you found it useful!
@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
@avrphreak9121
@avrphreak9121 2 жыл бұрын
Interesting! Keep it up, i subscribed !
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thanks for the sub!
@J_Creative
@J_Creative Жыл бұрын
Nice,ang clear thank you
@programmingelectronics
@programmingelectronics Жыл бұрын
Thanks!
@ArduBlock_Project
@ArduBlock_Project 2 жыл бұрын
С новым годом!
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Thanks!
@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!!
@AdeKingProductions
@AdeKingProductions 2 жыл бұрын
a >b ? thisShows : thatShows. The repeat of the condition in the statement can confuse some in its application. Great explanation though.
@umeshdange3844
@umeshdange3844 2 жыл бұрын
Best videos thanks sir
@xfran70
@xfran70 2 жыл бұрын
And what about " digitalWrite (ledPin, (input > 100)) " ?
@programmingelectronics
@programmingelectronics 2 жыл бұрын
Nice!
@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.
@rodrilea1
@rodrilea1 2 жыл бұрын
Thanks this makes sense. How does it work if you introduce delays between states?
@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! 👍
@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!!
@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!
@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.
@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.
@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.
@Ahmedhkad
@Ahmedhkad Жыл бұрын
I would writing digitalWrite(ledpin,(input>100) ? HIGH : LOW);
@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
Programming Electronics Academy
Рет қаралды 32 М.
How to Organize Code
14:37
Programming Electronics Academy
Рет қаралды 63 М.
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 35 МЛН
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 28 МЛН
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
Arduino Code: Conditional Statements
10:26
SparkFun Electronics
Рет қаралды 64 М.
Doing multiple timed things with Arduino: Unleash the millis()!
12:25
Programming Electronics Academy
Рет қаралды 124 М.
Why can't computers use base 3 instead of binary? Voltage states explained.
5:27
Basics Explained, H3Vtux
Рет қаралды 56 М.
HYDRAULIC PRESS VS TITANIUM AND STEEL ARMOR
8:42
Crazy Hydraulic Press
Рет қаралды 1,7 М.
Which Arduino IDE should I use?
13:43
Programming Electronics Academy
Рет қаралды 85 М.
Ternary Operators in C++ (Conditional Assignment)
8:01
The Cherno
Рет қаралды 125 М.
Using Arrays with Arduino
13:51
Programming Electronics Academy
Рет қаралды 30 М.
Serial Communication with Arduino - The details!
16:51
Programming Electronics Academy
Рет қаралды 63 М.
This mother's baby is too unreliable.
00:13
FUNNY XIAOTING 666
Рет қаралды 35 МЛН