Ternary Operator in Arduino C++ Language

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.

RELATED ARTICLES

Leave a comment

Your email address will not be published. Required fields are marked *

Please note, comments must be approved before they are published