Conditional Operator vs if else

198 Views Asked by At

Can we use multiple lines of statements inside the true section of conditional operator like in if else?

(a>b)? large = a; printf("%d",a) : large = b; printf("%d",b);

Can we use multiple line if statements in the true section of a ternary operator??

4

There are 4 best solutions below

3
Darshil Modi On

Yes, by using ,(comma) to separate expressions

(a > b) ? (large = a, printf("%d", a)) : (large = b, printf("%d", b));
1
Lundin On

The conditional operator is... an operator, and as such subject to the syntax rules of operators and expressions just like any other operator.

To include multiple sub-expressions in the same expression, we may use the , comma operator, which has left-to-right evaluation and returns the result of the right operand.

What you probably meant to do is something like this:

large = (a > b) ? (printf("%d\n",a), a) : (printf("%d\n",b), b);

Here we may note:

  • Operator precedence goes as > before ?: before = before , (the comma operator).

  • Therefore the parenthesis around (a > b) isn't actually necessary but makes the code more readable.

  • Since ?: has higher precedence than = we need not wrap the whole ?: in a parenthesis.

  • But since , has lower precedence than ?:, we must use inner parenthesis or we will get very strange results.

    For example if b>a and we have no parenthesis for the 3rd operator, then the printf statement will get taken as the result to be assigned and large will contain the return value of printf rather than b as expected.

  • This code is rather silly because we might as well write large = (a > b) ? a : b; and then print large. Way more readable.

  • This is an unreadable mess in general. We should avoid excessive use of the ?: and , operators since they tend to make code less readable. Their main usage is the niche topic of implementing function-like macros (which in itself should be avoided when possible). Using an if/else is almost always the preferred version.

4
gulpr On

You can but it is a terrible idea:

  • it is hard to read (generally almost all comma separated expressions are)
  • ternary operator was not invented for this purpose - basically to get value depending on condition.
large = (a > b) ? (printf("%d\n", a), a) : (printf("%d\n", b), b);

Instead of this monster you can simply:

large = (a > b) ? a : b;
printf("%d\n", large);
0
chqrlie On

You can combine multiple expressions inside the conditional operator branches using the comma operator, yet except in very specific circumstances, it is considered bad style, a form of code obfuscation:

    (a > b) ? (large = a, printf("%d", a)) : (large = b, printf("%d", b));

The value of the full expression is the return value of the printf call from the selected branch. Ignoring this value might let the compiler produce a warning.

Note that if large is defined as an int, this expression can simplified as:

    printf("%d", large = a > b ? a : b);

Still there is no reason to hide the side effect inside the printf argument. This is much more readable:

    large = a > b ? a : b;  // or better: large = max(a, b);
    printf("%d", large);