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?

-1
A ? B : C;

is shorthand for

if(A) {
    B;
} else {
    C;
}
  • 3
    It is not. B and C are not allowed to be statements or blocks, and if is not a valid RHS. – Ignacio Vazquez-Abrams Feb 16 '15 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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