Home » Courses » Conditionals 2: 'switch' » The Ternarator

Conditionals 2: 'switch'

1. Overview (01:59)
2. Switch (06:59)
3. Breaking The switch Down (11:06)
4. Switch In Action: Days Of The Month (13:09)
5. What if Can't Do... (09:17)
6. The Ternary Operation (:?) (07:39)
7. The Ternarator (06:53)

The Ternarator

In the previous video we have seen, how easily we could replace a 5-6 lines of if..else conditional with a single line of ternary operation. It is very easy and most of you might not understand why most of the experts avoid using this operator and does not recommend its use to anyone else either. This is because the ternary operation gets much complicated when we move to complex conditionals. Lets see how the complexity arises when we write an equation for a complex conditional.

Example for complex ternary operation

In the previous video, we used the example of finding the maximum value out of any two given values. But here we will write a program to obtain the maximum value out of any 3 given values. Once, the number of variables increase, the comparison becomes much harder and thus the ternary operation. The ternary equation for this can be written as a>b? (a>c? a:c) : (b>c?b:c); The logic can be formed using the nested if conditional. In the above equation, a>b? checks whether a is greater than b or not, if its true (a>c? a:c) is executed else (b>c?b:c) is executed. Now, if any one of the two sets is correct, we can find another ternary operation inside the brackets, just like another if statement inside a nested if conditional. So, again ternary operation is performed for these equations. Say a>b is satisfied and (a>c? a:c) is being processed. Now within this, the compiler checks whether a is greater than c or not. If its true, the value of a is traced out as output else the value of b is traced as output. Similarly, if the if the first condition is false then the second set of equation inside the bracket is executed. This is a bit complicated. The theory behind it is very simple yet the syntax for writing the ternary operation is difficult. So, most of the developers do not recommend the use of this technique.

Ternary Operation compared to if conditional

But one thing that needs to be remembered here is that, the whole operation is performed within a single line. If we go with if conditional for this, it takes about 10 lines approximately to develop the application to print the maximum value out of 3 numbers. Although it is simple and easy to understand, I think if the same operation can be performed in a single line, its much better. But its up to the individual, how he/she looks at it. If they feel that the code is too big compared to the complexity of the ternary operation, then they can obviously go for the ternary operations. But if you are a beginner, you will be good with if conditional and its good to prefer if rather than going for complex code in the beginning. So such beginners are advised not to use ternary operation when you are comfortable performing the same operation with if conditional. However, irrespective of whether you are learner or developer, it is important to know how these operations are performed. It just doesnt matter whether you use this technique or not but you must have sufficient knowledge on the topic. You should always be in a position to perform ternary operation any time if needed.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.