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

Поділитися
Вставка
  • Опубліковано 21 сер 2024

КОМЕНТАРІ • 70

  • @robotcantina8957
    @robotcantina8957 2 роки тому +4

    This is a slick way to compress the code, especially the one line variant.

    • @programmingelectronics
      @programmingelectronics  2 роки тому +1

      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 роки тому +1

      P.s. just noticed someone else had the same idea …

  • @bibel2k
    @bibel2k 2 роки тому +10

    I never saw this kind of coding!
    looks amazing and easy to understand!
    ‏thank you
    You got a thumbs-up and another subscriber.

  • @al-gaverlasaad5459
    @al-gaverlasaad5459 Рік тому +1

    The way you explain the code so mazing, super clear and easy to understand. New subscriber here.

  • @johnsun2416
    @johnsun2416 7 місяців тому +1

    Thanks for teaching, this is much better than if/else, where I don't have to deal with those pesky brackets.

  • @irgski
    @irgski Рік тому +1

    Very clear description! Thank you Sir!

  • @wilmercb
    @wilmercb 2 роки тому +4

    Great explanation! I hope you’re the one soon showing tutorials integrating Arduino and iOS/Swift code 😉

    • @programmingelectronics
      @programmingelectronics  2 роки тому +1

      Thanks for the recommendation on that Rikeõ! Is there any specific application you have in mind that you would like demoed?

    • @wilmercb
      @wilmercb 2 роки тому +1

      @@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 роки тому +1

      @@wilmercb Thanks! Yes that makes sense!

  • @user-fr3hy9uh6y
    @user-fr3hy9uh6y 2 роки тому +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.

  • @crisselectronicprojects8408
    @crisselectronicprojects8408 2 роки тому +2

    Great explanation!

  • @jumadhaheri
    @jumadhaheri 2 роки тому +2

    Happy New year thanks

  • @AdeKingProductions
    @AdeKingProductions 2 роки тому +2

    a >b ? thisShows : thatShows. The repeat of the condition in the statement can confuse some in its application. Great explanation though.

  • @warrenscorner
    @warrenscorner 2 роки тому +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

  • @michaelpopov1548
    @michaelpopov1548 Рік тому

    Simply great explanation!

  • @qcnck2776
    @qcnck2776 2 роки тому +3

    Nice explanation. Though I find the longer IF statement easier to read.

    • @programmingelectronics
      @programmingelectronics  2 роки тому +2

      Thanks! I kind of go back on forth on which I prefer...

    • @leitto-corleone
      @leitto-corleone 2 роки тому +3

      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

  • @vinothn4228
    @vinothn4228 2 роки тому +1

    Pure clearance man👌...

  • @J_Creative
    @J_Creative Рік тому +1

    Nice,ang clear thank you

  • @explorerpragun431
    @explorerpragun431 2 роки тому +3

    Thanks dude

  • @patrickmclaughlin6013
    @patrickmclaughlin6013 2 роки тому +4

    have to try, I think
    digitalWrite(ledPin, ((input>100) ? HIGH : LOW)));
    might work too?

    • @programmingelectronics
      @programmingelectronics  2 роки тому +2

      Nice, just checked - it works! Thanks Patrick! (small side note: one less parenthesis on the right.)

    • @henrybest4057
      @henrybest4057 2 роки тому +1

      @@programmingelectronics Also removes a variable (state) so will probably run a little faster, if that's important.

    • @LilGh02t
      @LilGh02t 2 роки тому +3

      the below statement also works for this case, is simpler, without the ternary operator.
      digitalWrite(ledPin, input>100);

    • @programmingelectronics
      @programmingelectronics  2 роки тому

      @@LilGh02t Nice!

  • @TinLethax
    @TinLethax 2 роки тому +1

    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.

  • @umeshdange3844
    @umeshdange3844 2 роки тому +1

    Best videos thanks sir

  • @avrphreak9121
    @avrphreak9121 2 роки тому +1

    Interesting! Keep it up, i subscribed !

  • @xfran70
    @xfran70 2 роки тому +3

    And what about " digitalWrite (ledPin, (input > 100)) " ?

  • @rehnmaak
    @rehnmaak 2 роки тому +1

    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.

  • @joshuapitong899
    @joshuapitong899 2 роки тому

    Thank you so much.❤

  • @Всемирная_Робототехника

    С новым годом!

  • @OffGridOverLander
    @OffGridOverLander 2 роки тому +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 роки тому +1

      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 роки тому +2

    Amazing

  • @rodrilea1
    @rodrilea1 2 роки тому

    Thanks this makes sense. How does it work if you introduce delays between states?

  • @TheJimtanker
    @TheJimtanker 2 роки тому +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 роки тому +1

      @@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 роки тому +2

    Is this in the arduino reference

  • @DrRusty5
    @DrRusty5 2 роки тому +2

    This really sorts of the mess of brackets required with if/else statements.

    • @programmingelectronics
      @programmingelectronics  2 роки тому

      Totally agree!

    • @DrRusty5
      @DrRusty5 2 роки тому +1

      @@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)