# what is a line of code like this called?

HIGH ? LOW : HIGH

A ternary operator. See for example http://cprogramminglanguage.net/c-ternary-operator.aspx

Pete

The expression

``````fred = (var == 1) ? HIGH : LOW;
``````

is functionally equivalent to the (longer) expression

``````if (var == 1)
fred = HIGH;
else
fred = LOW;
``````

marco_c: The expression

``````fred = (var == 1) ? HIGH : LOW;
``````

is functionally equivalent to the (longer) expression

``````if (var == 1)
fred = HIGH;
else
fred = LOW;
``````

And both are functionally equivalent to:

`````` fred = (var == 1);
``````

johnwasser:

marco_c: The expression

``````fred = (var == 1) ? HIGH : LOW;
``````

is functionally equivalent to the (longer) expression

``````if (var == 1)
fred = HIGH;
else
fred = LOW;
``````

And both are functionally equivalent to:

`````` fred = (var == 1);
``````

And finally equivalent to:

``````fred = var;
``````

And finally equivalent to:

``````fred = var;
``````

Not really.

If var were 2, fred would be 2 instead of 0 ;)

bibre: And finally equivalent to:

``````fred = var;
``````

Not really.

If var were 2, fred would be 2 instead of 0 ;)

Well you are right, by the made this ==1 is done, it's not the same... kinda `fred = !(var-1)`

But I think doing the "==1" step is just a loss of time. If 0 will be false/low, otherwise will be true/high. Like all nice Ansi C stuff :)

``````#define BADLUCK 13
void setup()
{
// This code will only run once, after each powerup or reset of the board
}

void loop()
{
// This code will loops consecutively
}
``````

That's true if the variable var is boolean. But if it is just a regular number, say int (=2 or =3,etc), you still need the (var==1).

¡Saludos de Monterrey! :)

As amusing as the banter is, it is probably confusing the OP who asked the question. HIGH and LOW are not defined in this case and could be 678 and 567 respectively, in which case the expression is not a simple assignment. The point is that the expression is generally used as a short form of the if statement.

True, true. Touché! XD

johnwasser: is functionally equivalent to the (longer) expression

The significant difference in my mind is that the ternary operator provides a single expression with a value containing the result. This means that it can be used as the right hand side of an assignment, and as the actual argument to a call, without having to assign the value to an intermediate variable.

Its called a conditional expression or the conditional operator. Ternary operator is a more generic term (triadic is an equivalent term). You could view the function call syntax fn(a,b) as a ternary operator for instance, and certainly in other languages there are a variety of ternary operators not related to conditional evaluation.

(Incidentally the general term I've seen for operators of multiplicity > 2 is "dist-fix" (short for distributed-fix, as opposed to prefix, postfix and infix). Prefix and postfix operators are termed unary or monadic, infix operators are binary(*) or dyadic, and the multiplicity is technically the arity of the operator. ?: has arity 3

(*) this can cause enormous confusion!