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

Switch In Action: Days Of The Month

<p>As we have seen in the <a href="/courses/video/9/71/Breaking-The-switch-Down.html">previous summary</a>, the 'break' keyword plays a very significant role on the flow of program control inside a <a href="/courses/video/9/70/Switch.html">switch construct</a>. To reiterate, the places where break is used inside a switch construct are the only exit points out of that switch, apart from the last case clause. If there are no break statements enclosed within successive case statements, control is said to <b>&quot;fall-through&quot;</b> to the next case, and the next and so on...</p> <p>So far we have viewed the break statement as we view the villain of a commercial pot-boiler movie: one which can do only harm and is a necessary evil. But the break statement, if intelligently used, can lead to very readable and efficient code.</p> <p>In fact, we'll now discuss an example where using an <a href="/course/basics/conditionals-if">if-conditional</a> is very cumbersome, and like in the <a href="/courses/video/9/71/Breaking-The-switch-Down.html">previous summary</a>, revolves around condition testing of a single <a href="/courses/video/5/157/Using-Variables.html">variable</a>. We'll also see that even when an if-construct is structured differently in this context, the switch still wins hands-down.</p> <p>Consider the following scenario:</p> <p>A student of elementary number theory wants to know whether the numbers from1 to 10 are prime or not. It so happens that his teacher has written a computer program that gives the correct answer when a number in the range [1, 10] is input to it. Its processing logic using an <a href="/courses/video/5/157/Using-Variables.html">if-else</a> construct would be something like:</p> <p><i>var num:int = …;</i></p> <p><i>if(num == 2 || num == 3 || num == 5 || num == 7){ </i></p> <p><i>trace(num + &quot;is primen&quot;); </i></p> <p><i>}else if(num == 4 || num == 6 || num == 8 || num == 9 || num == 10){ </i></p> <p><i>trace(num + &quot;is not primen&quot;); </i></p> <p><i>}else{</i></p> <p><i>trace(num + &quot;is God only knows whatn&quot;); </i></p> <p><i>}</i></p> <p>Most programmers very familiar with if-else constructs would be mighty proud of this code, but we claim it can be much improved upon. As we saw in a <a href="/courses/video/9/71/Breaking-The-switch-Down.html">previous summary</a>, a switch can enhance the readability of code involving the testing of a single identifier. Check this out:</p> <p><i>switch(num){ </i></p> <p><i>case 2: </i></p> <p><i>case 3: </i></p> <p><i>case 5: </i></p> <p><i>case 7: trace(num + &quot;is primen&quot;); </i></p> <p><i>break; </i></p> <p><i>case 4: </i></p> <p><i>case 6: </i></p> <p><i>case 8: </i></p> <p><i>case 9: </i></p> <p><i>case 10: trace(num + &quot;is not primen&quot;);</i></p> <p><i>break; </i></p> <p><i>default: </i></p> <p><i>trace(num + &quot;is God only knows what&quot;); </i></p> <p><i>}</i></p> <p>Look how cleverly we have used the explicit-exit-mechanism feature (the break) of the <a href="/courses/video/9/70/Switch.html">switch</a> construct to full advantage, making the code concise and easily readable. It would definitely look a bit cryptic to the novice programmer, but when you truly understand the logic behind it, you'll appreciate the fact that programming also involves an element of artistry: a major element, at that. Sloppy, spaghetti code is ugly, yes? Clean, flowing code is a thing of beauty.</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