As the name specifies ternary operator consist of three operand
- First is the boolean expression (condition) upon which the ternary operator works.
- Second is the statements which are executed if the boolean expression is true, and may be treated as body of if.
- Last is the statements which are executed if the boolean expression if false, and may be treated as body of else.
Syntax:
Basic syntax of ternary operator(boolean expression) ? (true-part) : (false-part)
Let’s see a basic example of ternary/conditional operator:
max = (a>b) ? a : b
The above expression returns the maximum value between a and b and stores in max. If the boolean expression is true i.e. if (a>b) then the expression will return a, else the will return b.
The above code is similar to
if(a>b) { max = a; } else { max = b; }
Note:
In C any non-zero integer value represents true and zero represents false.
Advantages of using ternary operator
- Reduces the code to single line or just few lines.
- Best for short conditions.
- If used with proper formatting it can more appropriate than simple if else.
Sample Program:
C program to find maximum between two numbers using ternary operator.#include <stdio.h> int main() { int num1, num2, max; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); max = (num1>num2) ? num1 : num2; printf("Max is %d", max); return 0; }Ouptut:
Enter two numbers: 20
30
Max is 30
30
Max is 30
NESTED TERNARY OPERATOR
Nesting of ternary operator is often dis-regarded by most of the programmer’s and even I also never recommend it. Nesting of ternary operators will definitely reduce the code length in characters but will also cost a poor readable code and more chances of error. Hence you must avoid a nested ternary operator as much as you can.Sample program of finding maximum between three numbers using a nested ternary operator
#include <stdio.h> int main() { int num1, num2, num3, max; printf("Enter three numbers: "); scanf("%d %d %d", &num1, &num2, &num3); max = ((num1>num2) ? ((num1>num3) ? num1 : num3) : ((num2>num3) ? num2 : num3)); printf("Max is %d", max); return 0; }In above program you can see that the ternary operator may had reduced the code length but had also made our program less readable. Hence to ensure readability of code we must use proper formatting for the ternary operator.
PROPER FORMATTING OF TERNARY OPERATOR
A proper formatted code will enhance your code readability and will also decrease the probability of errors in your code. A ternary operator must be well formatted in order to make it more readable and clear.Have a look to the poorly formatted ternary operator.
max = ((num1>num2) ? ((num1>num3) ? num1 : num3) : ((num2>num3) ? num2 : num3));
The above code can be formatted as
max = (num1>num2) ? ((num1>num3) ? num1 : num3) : (num2>num3) ? num2 : num3;
The above code demonstrates that how a simple formatted code has increased the readability of our program.
Note: Always try to use ternary operator when the condition is simple and small else use simple if else.