Go Down

Topic: Protothreading (Read 432 times) previous topic - next topic


I'm looking into protothreading: http://playground.arduino.cc/Code/TimedAction#Example

I understand for
Code: [Select]
for (First; Last; increment)
do while, and while loops.

I don't understand part of this bracket (from the example in the link above)
Code: [Select]
void blink(){
  ledState ? ledState=false : ledState=true;

I don't understand the layout of 
Code: [Select]
ledState ? ledState=false : ledState=true;



ledState is true?
If yes, set it to false
If no, set it to true

This statement inverts true to false or false to true. I would rather say ledState=!ledState; where the ! means NOT.


That's the ternary operator:

Code: [Select]
    boolean_value ? result_if_true : result_if_false;

They are using a round-about way of inverting the boolean value of ledState.  The easy way is:

Code: [Select]
   ledState = ! ledState;

They could also have used the operator more properly:

Code: [Select]
    ledState = ledState ? false : true;
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


Thank you very much guys!
liudr & johnwasser
I understand now.


I don't understand the layout of 
Code: [Select]
ledState ? ledState=false : ledState=true;

Yeah, the person who coded that probably didn't understand it either. It's a misuse of the ternary operator and a very contrived way to invert a boolean.
I only provide help via the forum - please do not contact me for private consultancy.




Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131