02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious Video

The Ternarator

<p>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.</p> <h2>Example for complex ternary operation</h2> <p>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&gt;b? (a&gt;c? a:c) : (b&gt;c?b:c); The logic can be formed using the nested if conditional. In the above equation, a&gt;b? checks whether a is greater than b or not, if its true (a&gt;c? a:c) is executed else (b&gt;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&gt;b is satisfied and (a&gt;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.</p> <h2>Ternary Operation compared to if conditional</h2> <p>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.</p>

Overview

We'll look at the drawbacks and lack of flexibility of the if-else and else-if construct, and propose an alternative which is more flexible and powerful

01:59

Switch

We'll introduce you to the switch construct, explaining how it can be used to make decisions based on checking and comparisons using a single identifier

06:59

Breaking The switch Down

We'll discover and describe in detail the importance of using break statements in switch cases, and the pitfalls if we forget/choose not to do so. Let us learn about the break keyword in detial

11:06

Switch In Action: Days Of The Month

We will see a few code examples in as3 to illustrate certain programming logic which can be implemented very easily and concisely using switch constructs

13:09

What if Can't Do...

We'll see unique programming logic which is elegantly and efficiently achieved using switch constructs, but are next to impossible with if constructs

09:17

The Ternary Operation (:?)

Enhancing the readability of code and introducing efficiency for simple decision making scenarios using ternary operator ?: We will also learn the syntax for using ternary operator

07:39

The Ternarator

In this video, we will see how to use ternary operator for complex conditionals. We will compare it by if conditional and discuss the necessity for understanding the concept and its implementation in

06:53