Go Down

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

InduinoX

Jan 13, 2013, 04:23 pm
digitalWrite(13, on ? HIGH : LOW);

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

Arrch

#1
Jan 13, 2013, 04:32 pm
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 pmLast 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

#3
Jan 13, 2013, 08:04 pm

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

#4
Jan 13, 2013, 09:23 pm
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

#5
Jan 13, 2013, 11:00 pm
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

Please enter a valid email to subscribe