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

A ? B : C;

is shorthand for

if(A) {
} else {
  • 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

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.


Your Answer

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

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