Understanding the Ternary Operator in C/C++

Filed Under: C Programming
Ternary Op C Cpp

In this article, we’ll take a look at understanding the ternary operator in C / C++.

The ternary operator (? :) is a very useful operator, which is very similar to if-else statements. This is a useful option for programmers, to avoid cluttering the code with nested if-else conditions.

Let’s take a quick look at using this simple operator, using some illustrative examples!


The Ternary Operator

Before coming to the ternary operator, let’s look at the format for a simple if-else statement.

if (condition) {
    variable = expression1;
}
else {
    variable = expression2;
}

While this is simple enough, it might be more convenient to write it in one line and assign to a variable directly!

We can do this through the ternary operator.

The ternary operator works like this and is the same as the above if-else block.

variable = (condition != 0) ? expression1 : expression2;

See how simple it is?!

What this says is that variable will be assigned the value of expression1 if condition holds true. Otherwise, it will be given the value of expression2.

So, this is essentially the same as the original ‘if-else’ statement.

Let’s now look at a simple example.


Using the Ternary Operator – Some Examples

Consider the following program, which will assign variables to -1, based on whether it is odd or even.

#include <stdio.h>

int main() {
    int a = 10;
    int b = 5;

    int c = (a % 2 == 0) ? a : -1;
    int d = (b % 2 == 0) ? b : -1;

    printf("a = %d, c = %d\n", a, c);
    printf("b = %d, d = %d\n", b, d);

    return 0;
}

Output

a = 10, c = 10
b = 5, d = -1

As you can see, since a is even, the first part (after ?) is now evaluated and assigned to c.

While in the second case, since b is odd, the second part (after :) is now evaluated and assigned to d.

Ternary operators also make writing recursive functions very easy to read.

Consider the below program, which uses a recursive function to find the fibonacci sequence, starting from 1, using ternary operators!

The recursive function will return 1 if n <= 1, and return fib(n-1) + fib(n-1) otherwise!

#include <stdio.h>

int fibonacci(int n) {
    return (n <= 1) ? 1 : fibonacci(n-1) + fibonacci(n - 2);
}

int main() {
    int val = fibonacci(10);
    printf("Starting from 1, fibonacci(%d) = %d\n", 10, val);
    return 0;
}

Output

Starting from 1, fibonacci(10) = 89

If you work it out, you can indeed verify that the 10th fibonacci number after 1 is 89.

The recursive function is now a simple one-liner due to the ternary operator.

Hopefully, this gives you another reason to use ternary operators more often!


Conclusion

We learned about using the Ternary operator (? :), which is a nifty trick using which we can avoid writing nested if-else conditions.

This makes writing simple conditional statements in a single line, which is a good option to have for C / C++ programmers.

References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages