Check The Programming Section
C/C++ have another powerful operator called conditional operator. The conditional operator forms with ? and : are sometimes called ternary operators, because they take three operands. This operator replace certain type of if-than-else statement and it takes the following general form:
Expression1? Expression2: Expression3;
The ternary ?: operator works like that, if the Expression1 evaluates to true, then Expression2 gets evaluated and becomes the value of the expression. If Expression1 evaluates to false, then Expression3 gets evaluates and becomes the value of the expression. For example,
int main(){
int a = 3;
a>2 ? printf("Value of a is largest") : printf("Value of a is smallest");
}
After the evaluating of the above expression a>2 it evaluates to true, because value of a is 3, which is greater than 2. So, in this Expression1 evaluates to true, then Expression2 printf("Value of a is largest") gets executed and prints this message to the screen as value of the expression. Let's take another example,
int main(){
int a = 4;
int k = a>10 ? 100 : 200;
printf("%d", k);
}
In this example, value of 200 is returned as the value of k. Because, Expression1 a > 10 evaluates to false and Expression3 gtes executed and becomes the value of the expression.
To do this, add proper parentheses and invert the order of the assignment and the output insertion (when using the comma operator, the value of the left expression is discarded):
#include <stdio.h>
int main(){
4<5?(printf("Hi"),printf("%d",2)):printf("%d",78);
return 0;
}
However, the advice is to make your code readable and abandon this kind of coding style.
Ternary operator can be nested. A nested ternary operator can have many forms like :
a ? b : c
a ? b ? c : d : e
a ? b ? c : d ? e : f : g
1. a? b : c
Example - 1: The above synatx is a simple conditinal operator statement without any nesting. Consider the example below, to understand this.
#include <stdio.h>
int main(){
int a = 5, b =6;
int max = a>b ? a : b;
printf("%d",max);
return 0;
}
This program will check the maximum value of a and b. At first the expression a>b will be eavluted to false. So the third expression after colon (:) will be evaluted and the value of b will be returned to variable max.
Example - 2: Now interchange the value of a and b and then compile
#include <stdio.h>
int main(){
int a = 6, b = 5;
int max = a>b ? a : b;
printf("%d",max);
return 0;
}
The above program will check the maximum value of a and b. At first the expression a>b will be eavluted to true. So the second expression after question mark (?) will be evaluted and the value of a will be returned to variable max.
2. a ? b ? c : d : e
In this case, first condition a will be evaluted and if it is true then condition b will be evaluted else statement written as part of e will get executed. If the codition b is evaluted to true then expression written as part of c will be evaluted else expression written as part of d will be returned.
Example -1: Get the sum of two variable c and b if a is greater than b and a is divissible by 3. If a is not divissible by 3 then perform multiplication of a and b and print the result. If a is less than b then print the value of b.
To solve this problem we need to write two level of conditional statement using conditiona operator(?:). At first, (a>b), will be placed as a in the above syntax. It will check the value of a is greater than b or not. Based on that there is two possibilities as follows:
True: Then we need to check a%3==0 or not and this condition need to be placed as b, as follows
a>b?a%3==0?
Further we need to check the case for a%3==0 is true or false and again there is two possibilities, as follows:
1a. True: Then write the code after the second question mark (?) ro calculate the addition between b and c. As follows:
a > b ? a % 3 == 0 ? printf("%d",b + c)
1b. False: Then write the code for multiplication for false case as the continution of the previous statement by adding a colon (:). As follows:
a > b ? a % 3 == 0 ? printf("%d",b + c) : printf("%d", a * b)
False: Now only false part for the condition a > b need to be added with the above condtional statement using a colon (:). As follows:
a > b ? a % 3 == 0 ? printf("%d",b + c) : printf("%d", a * b) : printf("%d",b);
We can also use parentheses to write nesting conditional statement and make the code readable and understanble. As follows,
((condition1)?((condition2)?(code for true case of condition2): (code for false case of condition2)):code for false case of condition1)
The only point we need to taken care, we need to use a parentheses and it will remain open till the writting of false case for that condition using colon.