Conditional operators in C is also known as ternary operators. It is used to check conditions and based on the result the next line of code is executed.
Syntax:
Condition ? expr_If_True : expr_If_false
Arguments of Condition Operator
Condition
It is an expression that is evaluated to obtain a value. The value obtained can be either true or false.
expr_If_True: this is an expression that is evaluated when the condition results in true value.
expr_If_false: this expression is evaluated when the condition results in a false value.
Condition operators become useful when the result of the expression has to be assigned to a variable. To assign value to a variable, the syntax is:
variable = Condition ? expr_If_True : expr_If_false
The advantage of the above-stated syntax is that in a single line of code condition is evaluated and its resultant is assigned to a variable. Thus, it saves extra lines of code and improves code compilation time further it improves the efficiency of memory management techniques. When the number of lines of code is large, then the Condition Operator improves the readability of the code. The efficiency of the conditional operator is at its maximum for lengthy codes.
Condition operators can not be used for complex logic. If complex logic has to be written then Condition Operators are replaced by “If statements”.
Conditional Operator program in C
Following code display the use of condition operator:
Code Block 1:
#include <stdio.h> int main( ) { int age; printf("Enter your age"); scanf("%d", &age); age<18?printf("You are still a child."): printf("You are no longer a child."); return 0; }
Output: Enter your age: 23 You are no longer a child.
Code Analysis:
In the above code, the Condition Operator is used. It can be observed that it has a condition “age < 18” which is evaluated using the compassion operator “<”. If it is true, then the first expression following the condition is executed, that is printf(“You are still a child”) is executed otherwise printf(“You are no longer a child”) is executed.
Code Block 2:
#include <stdio.h> int main( ) { int a,b; printf("Enter the basic pay"); scanf("%d", &a); a<1800?printf("You will get hra.", a+10000):printf("No HRA"); return 0; }
Output: Enter basic pay: 1200 You will get hra: 11200
Nested Conditional Operator
Conditional operators can also be nested. Nesting can be done by including condition expression as a second statement.
Example of nested Conditional Operator:
Code Block – 3
#include <stdio.h> int main( ) { int a=1,b=2,result; result = (a == 1? (b == 2 ? 3 :5) : 0); printf("%d\n", result); }
Output: 3
Execution pattern of Condition Operator
Conditional expressions are evaluated using precedence and associative rules. “( )” parentheses are used to determine precedence and associativity. The conditional operator requires three operands and “( )” contained within the Conditional Operator determine order of execution. Condition Operators follow the right associative rule.
For example condition operator,
a ? b : c? d : e will be evaluated as,
a ? b : ( c ? d : e ) ,not as,
( a ? b : c ) d : e
Leave a Reply