Programmers use the ternary operator for decision making in place of longer if and else conditional statements.
The ternary operator take three arguments:
- The first is a comparison argument
- The second is the result upon a true comparison
- The third is the result upon a false comparison
It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here’s a simple decision-making example using if and else:
int a = 10, b = 20, c;
if (a < b) {
c = a;
}
else {
c = b;
}
printf("%d", c);
This example takes more than 10 lines, but that isn’t necessary. You can write the above program in just 3 lines of code using a ternary operator.
Syntax
condition ? value_if_true : value_if_false
The statement evaluates to value_if_true if condition is met, and value_if_false otherwise.
Here’s the above example rewritten to use the ternary operator:
int a = 10, b = 20, c;
c = (a < b) ? a : b;
printf("%d", c);
Output of the example above should be:
10
c is set equal to a, because the condition a < b was true.
Remember that the arguments value_if_true and value_if_false must be of the same type, and they must be simple expressions rather than full statements.
Ternary operators can be nested just like if-else statements. Consider the following code:
int a = 1, b = 2, ans;
if (a == 1) {
if (b == 2) {
ans = 3;
} else {
ans = 5;
}
} else {
ans = 0;
}
printf ("%d\n", ans);
Here's the code above rewritten using a nested ternary operator:
int a = 1, b = 2, ans;
ans = (a == 1 ? (b == 2 ? 3 : 5) : 0);
printf ("%d\n", ans);
The output of both sets of code above should be:
3
Recommended courses from our partners (sidebar)
-
On Scrimba
Learn JavaScript
Per Borgen
9h 21m
9 hours 21 minutes
Beginner
View course
(opens in a new tab)
-
On Scrimba
Learn HTML and CSS
Per Borgen
5h 40m
5 hours 40 minutes
Beginner
View course
(opens in a new tab)
-
On Udemy
Python Mega Course : Go Beginner to Expert in Python3
Funky Programmer
4 out of 5 stars (2,586) from 2,586 ratings
11h 5m
11 hours 5 minutes
Beginner
Certificate
View course
(opens in a new tab)
-
On Udemy
PYTHON - A to Z Full Course for Beginners
Joydip Ghosh and Arjun Reddy
4.1 out of 5 stars (1,376) from 1,376 ratings
1d 48m
1 day 48 minutes
Beginner
Certificate
View course
(opens in a new tab)
If you read this far, thank the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Recommended courses from our partners
-
On Scrimba
Learn JavaScript
Per Borgen
9h 21m
9 hours 21 minutes
Beginner
View course
(opens in a new tab)
-
On Scrimba
Learn HTML and CSS
Per Borgen
5h 40m
5 hours 40 minutes
Beginner
View course
(opens in a new tab)
-
On Udemy
Python Mega Course : Go Beginner to Expert in Python3
Funky Programmer
4 out of 5 stars (2,586) from 2,586 ratings
11h 5m
11 hours 5 minutes
Beginner
Certificate
View course
(opens in a new tab)
-
On Udemy
PYTHON - A to Z Full Course for Beginners
Joydip Ghosh and Arjun Reddy
4.1 out of 5 stars (1,376) from 1,376 ratings
1d 48m
1 day 48 minutes
Beginner
Certificate
View course
(opens in a new tab)