@etrigo (etrigo) posted a new topic, subject "Code interpretation project". It read:

   Hey guys.  
    I'm doing a project but I came across this line and I'm in doubt what it does.  
    The line in question is line 4  
    'serial_byte = (serial_byte <0)? 0: ((serial_byte> 180)? 180: serial_byte); '
    
    can you help me know what this line says?
    [code] 
        void read_serial_byte_set_servo(Servo& servo, bool invert)
        {
          serial_byte = Serial.read();
          serial_byte = (serial_byte < 0) ? 0 : ((serial_byte > 180) ? 180 : serial_byte);
          if (invert)
            servo.write(180 - serial_byte);
          else
            servo.write(serial_byte);
        }
        
    [/code]

Then @wildbill (wildbill) said

    It's a couple of if statements, made more confusing. Look up the C++ Ternary operator.

Then @anon73444976 said

    What doubts do you have?  
    If it does what you want,there should be no need for doubt.
    
    If you simply don't understand it, tell us which bit you don't understand.
    
    If serial_byte really is a byte, there should be no need to see if it is less than zero

Then @etrigo (etrigo) said

    If you do what I want, I would have no doubts to interpret.  
    My question is in the topic. Thanks for reading.

To which @etrigo (etrigo) replied

    thanks. from what I understand it looks like one inside the other

So @anon73444976 replied

    It's simply ensuring serial_byte is in the range 0 to 180, and clipping it to those values if not.
    
    As I wrote, the "<0" test is probably redundant.

To which @etrigo (etrigo) replied

    Thank you all.  
    I've already figured it out. I didn't know what this was.  
    It is a Ternary Operator. In this case, one inside the other.  
    I realized thanks to everyone  greetings

So @system (system) replied

    This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://forum.arduino.cc/t/code-interpretation-project/862097', 'title': 'Code interpretation project', 'views': 233, 'has_accepted_answer': True, 'word_count': 254, 'like_count': 2, 'reply_count': 3, 'participant_count': 3, 'posts': 8, 'tags': '', 'author': '@etrigo (etrigo)', 'participants': '@etrigo (etrigo), @wildbill (wildbill), @anon73444976, @system (system)', 'image_url': [None], 'created_at': '2021-05-15T13:32:50.444Z', 'last_posted_at': '2021-09-12T13:47:04.124Z'}