This C Tutorial explains the Conditional Operator in C with examples.
What is Conditional Operator in C?
The conditional operator, also known as the ternary operator, is a operator that takes three operands. The first operand is a boolean expression, and the second and third operands are values. If the boolean expression evaluates to true, then the second operand is returned, otherwise the third operand is returned.
Syntax of the conditional operator is:
expression1 ? expression2 : expression3; (or) condition ? value1 : value2
Besides Operator Precedence and Associativity, It, like the logical operators, does exert some control on the order of evaluation of the entire expression. For example:
Example 1:
(number >= 1000) ? a + b + c : d + e + f;
Here in the above example, if (number >= 1000) is TRUE then result of whole expression is expression2 which is (a + b + c) and expression3 (d + e + f) is not evaluated at all. But if (number >= 1000) evaluates to FALSE then result of the entire expression is expression3 which is (d + e + f) and expression2 isn’t evaluated at all.
Example 2:
Here is another example of how the ternary operator can be used:
#include <stdio.h> void main() { int k = 8, m = 7, result; result = k < m ? k++ : m + 4; printf("%d", result); }
In this case, the condition is k < m, i.e 8 < 7 which results in false.
As a result, the answer is m + 4 = 7 + 4 = 11.
Output:
11
If-else statement:
Basically, conditional operator is same as if-else construct. We can rewrite the above conditional expression as:
if (expression1) expression2; else expression3;
Advantages of conditional operator over if-else construct:
Then what is the difference between conditional expression and if-else statement. Firstly, conditional expression is little bit more compact than if-else construct. For example:
/* b written twice using if-else */ if (ch == 'A') b = 5; else b = ch + 5; /* b written once using conditional operator */ b = (ch == 'A') ? 5 : ch + 5;
Consider another example:
/* subscript written twice using if-else */ if (a > 10) b[a + c / 5 * 10 - 2] = 100; else b[a + c / 5 * 10 - 2] = 1000; /* subscript written once using ternary operator */ b[a + c / 5 * 10 - 2] = a > 10 ? 100 : 1000;
Notice in the above example, subscript is a complex exp. and there are chances of errors while typing it twice as in if-else. So, using the conditional statement, we can avoid such errors.
Related Articles
- Practice more conditional expressions program here.
- Conditional Expressions MCQs
- Conditional Statements Usage in C
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Buy Computer Science Books
- Buy C Books
- Practice BCA MCQs