2

Going through the "timeouts" example of the elapsedMillis library (https://github.com/pfeerick/elapsedMillis) I found the following statement:

digitalWrite(LEDPIN, flashPattern[ledIndex++] ? ledBrightness : !ledBrightness);

  • flashpattern is an array of boolean variables (boolean flashPattern[] = { 1,1,1,1,1,1,1,0,1,1,0 };)
  • ledBrightness is a boolean as well.

What does the "?" and the ":" do there? are these boolean operators?

2 Answers 2

-1
A ? B : C;

is shorthand for

if(A) {
    B;
} else {
    C;
}
1
  • 3
    It is not. B and C are not allowed to be statements or blocks, and if is not a valid RHS. Feb 16, 2015 at 20:48
5

They are the conditional operator. If the expression before the ? is true then the result is the expression before the :, otherwise it is the expression after.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.