Dipankar Shaw

Posted on

# Stop using if...else βπ«

Stop using if...else π« for every thing. There is a better option than if...else which is simpler to write and less line of code. This alternative is called Ternary Operator. C, C++, Java, Javascript and many more programming language have this operator.

## π€·ββοΈ What is ternary operator ?

Ternary operator is a decision making or conditional operator which is frequently used as the alternative of if...else. In mathematics, a ternary operation is an n-ary operation with n = 3 from there we get the name, an operator that takes three arguments. sometimes it takes more than three argument we will talk about that later.

## β How to write it ?

writing a ternary is as easy as writing if...else typically both are same just some symbols are used instead of if & else keywords.

``````syntax
condition ? True expression : False expression
``````

a condition followed by a question mark (?), then an expression to execute if the condition is true followed by a colon (:), and finally the expression to execute if the condition is false. Then end it with semicolon (;) if required.

## How to assign value to a variable using this ?

You are probably thinking π€ if we are want to find the greatest number in two numbers and store it to a variable then we have to write this m > n ? max = m : max = n no there is better option.

`max = m > n ? m : n`

Here the greatest number will automatically assign to max, we do not need to do max = m or max = n which we do in case of if...else.

## π Chained ternary operator

The ternary operator can be "chained" π similar to an if β¦ else if β¦ else if β¦ else chain in the following way:

``````condition1 ? value1
: condition2 ? value2
: condition3 ? value3
: value4;
``````

## Examples

Let's have some example to practice the ternary operator.
Here all the example are written in C language. C++ and JavaScript both have same syntax as C the only difference is in variable declaring and printing something in the screen.

### Example 1: Find the greatest number between m and n.

``````int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d", m, n)
: printf("n is greater than m that is %d > %d", n, m);
#### Output
m is greater than n that is 5 > 4
``````

### Example 2: Find the smallest number between m and n.

``````int m = 5, n = 4, min;
min = (m < n) ? m : n;
printf("%d is the smallest number between %d and %d", min, m, n);
#### Output
5 is the greatest number between 5 and 4
``````

### Example 3: chained ternary operator

``````int i = 13;
i < 10 ? printf("%d is smaller than 10", i)
: i == 10 ? printf("%d equals to 10", i)
: i > 10 && i < 20 ? printf("%d is greater than 10 but less than 20", i)
: printf("%d is greater than 20", i);
#### Output
13 is greater than 10 but less than 20
``````

## Why you need this ?

I am not saying that If...else is useless, you can use them anywhere you want. There is a vary big but π. You can not use it everywhere. If you are a react developer than you will know what I am saying. Since ternary operator can be used within the return statement of a JavaScript function, in react like library or framework ternary operator is used most. So knowing this concept will be helpful for you.