Conditional operator
- The same algorithm that tests if a number is odd or even, this time with the '?:' operator (ternary or conditional operator):
int a;
a % 2 == 0 ? printf("the number %d is even", a) : printf("the number %d is odd", a);
The '?:' operator simplifies the 'if else' statements into a single line of code, an expression formed through one conditional and two commands. There are two differences between using 'if else' or the conditional operator: readability and debugging. For the former, a single line is more close to natural language. For the latter, it might be necessary to have the 'if else' statements to debug the program's flow.
There is a caveat though: 'if else' is a pair of statements, whereas '?:' is an operator. In plain english: whenever the conditional expression is formed through a question and there is a finite number of possible answers (questions that lack an answer doesn't count), then it's possible to write the same thing using the conditional operator. However, if the 'if else' pair is used to control the program's flow, such as to break a loop, then there's no corresponding expression using the conditional operator. That derives from a very subtle difference: expressions are something to be evaluated (the whole line of code using the conditional operator is considered an expression). There is a result associated to it, whereas statements doesn't incur an evaluation per se. Variables can be declared and statements such as 'break' and 'return' can be used inside 'if else' blocks, but not inside conditional expressions. On the opposite side, the conditional operator can be used inside 'if else' and even be the conditional expression of an 'if' statement itself.
Another case in which 'if else' isn't directly translated to a conditional operator expression: there are cases in which there's an if clause with no else clause, or more than one if clause and all of them can be simultaneously true. In such scenario the expression that effectively replaces the absent else clause is any dummy expression such as a = a. To add a dummy expression to fill in the missing else clause when using the conditional operation is a waste of computational time.
Performance wise, the conditional operator may or may not be exactly the same as 'if else'. The low level code would have to be checked to see if there are any differences between the two. Although, for this introductory level, performance doesn't matter.
- A simple variation of the algorithm above:
int a, b;
b = (a % 2 == 0) ? 1 : 0;b == 1 ? printf("the number %d is even", a) : printf("the number %d is odd", a);
This is the same thing as using two 'if else' statements, one after the other. The first tests whether 'a' is even or odd and stores the "answer" in 'b', then the second prints the message. Nested cases such as in the example above about the sum and product of even and odd numbers can also be translated to conditional operator's syntax. But they become harder to read and more prone to errors.
- if ... else if ... else' statements can also be converted to the '?:' syntax:
int a;
(a > 0) ? printf("number %d is positive", a) :(a < 0) ? printf("number %d is negative", a) : printf("number %d is neither positive nor negative", a);
Notice that the the ':' sign appears twice. The first time for the first 'if'. The second time it separates the 'else if' (to the left) from the 'else' (to the right).
The main disadvantage of the conditional operator for nested 'if else' constructions is that it may be harder to read the code.