3

This is more of a theoretical question.

Is it possible to use ternary operators, like it is possible in C (e.g. (a<b) ? a : b), and if yes how?

  • Are you looking at using an input like \evaluate{a<b ? a : b}? – Werner Nov 2 '16 at 21:31
  • @Werner I'd appreciate an explanation on how this works. but I was more interested in how ternary operators can be used as a tool of programming. – naphaneal Nov 2 '16 at 21:48
8

Yes C only needs different syntax for ? and if because it distinguishes between statements and expressions. programming languages that don't make that distinction or don't make it so strongly, can naturally use the value of an if-expression.

so you do not have to go

\ifnum \count0<\count2
  \count3=4
\else
  \count3=5
\fi

which is the analogue of a C if surrounding two assignments.

You can go

\count3=\ifnum \count0<\count2 4\else 5\fi

with the if expression providing the value of a single assignment which is the analogue of a C ternary expression.

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.