Go Down

Topic: What does this mean? (Read 526 times) previous topic - next topic

InduinoX

digitalWrite(13, on ? HIGH : LOW);

It was in the IRrelay example.What does the ? do and the : with both LOW & HIGH.

Arrch

http://en.wikipedia.org/wiki/%3F:#C.2B.2B

It's the functional equivelent to this:

Code: [Select]
if (on)
  digitalWrite(13, HIGH);
else
  digitalWrite(13, LOW);

guix

#2
Jan 13, 2013, 05:09 pm Last Edit: Jan 13, 2013, 05:11 pm by guix Reason: 1
on is probably a boolean (either true or false) or an int either 1 or 0, so it is equivalent to:
Code: [Select]

digitalWrite(13, on);

PeterH


on is probably a boolean (either true or false) or an int either 1 or 0, so it is equivalent to:
Code: [Select]

digitalWrite(13, on);


That code makes the assumption that HIGH and LOW are the same values as true and false. It just happens to be so in this case, but as a general rule it's best to avoid making that sort of assumption - the original implementation is preferable IMO.
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

Quote
That code makes the assumption that HIGH and LOW are the same values as true and false.

I don't think it does.
Last time I looked at the source of digitalWrite, it very specifically incorporated the C convention of zero / non-zero as meaning LOW / HIGH.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dhenry

Quote
What does the ? do and the : with both LOW & HIGH.


If variable "on" has any value other than 0, set the pin. Otherwise (when "on" is 0), clear the pin.

Go Up