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

Поделиться
HTML-код
  • Опубликовано: 7 янв 2025

Комментарии •

  • @robotcantina8957
    @robotcantina8957 3 года назад +4

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

    • @programmingelectronics
      @programmingelectronics  3 года назад +1

      I agree! Thanks for watching!

    • @RobiBue
      @RobiBue 3 года назад

      @@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 3 года назад +1

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

  • @bibel2k
    @bibel2k 3 года назад +9

    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 2 года назад +1

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

  • @johnsun2416
    @johnsun2416 11 месяцев назад +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!

  • @warrenscorner
    @warrenscorner 3 года назад +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  3 года назад

      Thanks for watching Warren! I see the ternary pop up in code now and again, so it's nice to recognize it.

    • @yashaswikulshreshtha1588
      @yashaswikulshreshtha1588 3 года назад

      @@programmingelectronics Hey love your video. Can you please make a video for pointers and unions etc?

    • @yashaswikulshreshtha1588
      @yashaswikulshreshtha1588 3 года назад

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

  • @wilmercb
    @wilmercb 3 года назад +4

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

    • @programmingelectronics
      @programmingelectronics  3 года назад +1

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

    • @wilmercb
      @wilmercb 3 года назад +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  3 года назад +1

      @@wilmercb Thanks! Yes that makes sense!

  • @vinothn4228
    @vinothn4228 3 года назад +1

    Pure clearance man👌...

  • @jumadhaheri
    @jumadhaheri 3 года назад +2

    Happy New year thanks

  • @michaelpopov1548
    @michaelpopov1548 2 года назад

    Simply great explanation!

  • @qcnck2776
    @qcnck2776 3 года назад +3

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

    • @programmingelectronics
      @programmingelectronics  3 года назад +2

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

    • @leitto-corleone
      @leitto-corleone 3 года назад +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

  • @xfran70
    @xfran70 2 года назад +3

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

  • @crisselectronicprojects8408
    @crisselectronicprojects8408 3 года назад +2

    Great explanation!

  • @user-fr3hy9uh6y
    @user-fr3hy9uh6y 3 года назад +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.

  • @avrphreak9121
    @avrphreak9121 3 года назад +1

    Interesting! Keep it up, i subscribed !

  • @AdeKingProductions
    @AdeKingProductions 3 года назад +2

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

  • @patrickmclaughlin6013
    @patrickmclaughlin6013 3 года назад +4

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

    • @programmingelectronics
      @programmingelectronics  3 года назад +2

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

    • @henrybest4057
      @henrybest4057 3 года назад +1

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

    • @LilGh02t
      @LilGh02t 3 года назад +4

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

    • @programmingelectronics
      @programmingelectronics  3 года назад

      @@LilGh02t Nice!

  • @J_Creative
    @J_Creative 2 года назад +1

    Nice,ang clear thank you

  • @rodrilea1
    @rodrilea1 3 года назад

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

  • @OffGridOverLander
    @OffGridOverLander 3 года назад +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  3 года назад +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 3 года назад

      @@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! 👍

  • @TinLethax
    @TinLethax 3 года назад +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.

  • @TheJimtanker
    @TheJimtanker 3 года назад +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  3 года назад

      Thanks Jim!

    • @TheJimtanker
      @TheJimtanker 3 года назад +1

      @@programmingelectronics I love my KOAS and still use it years later, especially when I'm showing other people what Arduinos can do.

  • @umeshdange3844
    @umeshdange3844 3 года назад +1

    Best videos thanks sir

  • @rehnmaak
    @rehnmaak 3 года назад +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.

  • @DD-jj2tc
    @DD-jj2tc 3 года назад +2

    Is this in the arduino reference

  • @joshuapitong899
    @joshuapitong899 2 года назад

    Thank you so much.❤

  • @explorerpragun431
    @explorerpragun431 3 года назад +3

    Thanks dude

  • @ArduBlock_Project
    @ArduBlock_Project 3 года назад +2

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

  • @DrRusty5
    @DrRusty5 3 года назад +2

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

    • @programmingelectronics
      @programmingelectronics  3 года назад

      Totally agree!

    • @DrRusty5
      @DrRusty5 3 года назад +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.

  • @electronics8627
    @electronics8627 3 года назад +2

    Amazing

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