(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
there are many other lines in the code but i coudnt comprehend this line. please if anyone would explain.......
(switch_state == false) ? digitalWrite(gpio_switch, LOW) : digitalWrite(gpio_switch, HIGH);
there are many other lines in the code but i coudnt comprehend this line. please if anyone would explain.......
it called "ternary operator"
thank you so much
It's actually a very verbose way of writing
digitalWrite(gpio_switch, switch_state ? HIGH: LOW) ;
Where did you find it?
That's shockingly bad.
The only reason I can think of someone writing it that poorly is that it shows up easily in Google searches, as an anti-plagiarism measure.
And for 99% of Arduinos, that's a slightly verbose way of saying:
digitalWrite(gpio_switch, switch_state) ;
This is how it starts. condition ? if true : if false; similar to x = x + 1; Through experience we gather x++ or x+=1 and if (x = 1) is if (x). Can a post-solution for the OP also be digitalWrite(gpio_switch, switch_state ^= 0);?
For me, this is where the statement that C++ is a strongly-typed language falls apart.
![]()
It was originally as --
if(switch_state == false)
{
digitalWrite(gpio_switch, LOW);
}
else
{
digitalWrite(gpio_switch, HIGH);
}