Free help & advice Talk to us

  • Free shipping over A$55

    Within Australia

  • Free help & advice

    We reply within 24 hours

  • 30-day returns

    12-month warranty on faults

  • Ships next business day

    When the item is in stock

Ternary Operator in Arduino C++ Language

Ternary Operator in Arduino C++ Language

Lonely Binary |

The Ternary Operator provides a shorthand method to express simple if-else statements. This operator consists of a question mark (?) and a colon (:). This operator perform an operation that evaluates a condition and returns one value if the condition is true, and another value if the condition is false.

Ternary Operator

x < 0 ? y = 10 : z = 20;

is equal to

if (x < 0)
    y = 10;
else
    z = 20;

The ternary operator can be used in assignments.

a = (x > 100) ? 0 : 1;

is equal to

if (x > 100)
    a = 0 ;
else
    a = 1;

 Let's use this ternary operator in Arduino IDE.

Leave a comment

Please note: comments must be approved before they are published.