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

What if Can't Do...

<p>We'll now look at an example which can only be elegantly written using a <a href="/courses/video/9/70/Switch.html">switch construct</a>. It's our usual unusual example, which generally appeals to the nut within!</p> <p>Consider the following scenario: A special kind of ranking or value points tally is being maintained for instruments within an art gallery, to entertain elementary school kids. The ranking system tries to classify instruments in a hierarchy, and specialization is the aim. A more specialized instrument (under the classification hierarchy, which we will show in a moment) gets a cumulative-rank which is the sum of the ranks of the categories it's already part of, plus its own rank. That's, each instrument gets points for each category it belongs to. For example, consider the following trail of classification:</p> <p>Bass-Guitar (is a) Guitar (is a) Stringed-Instrument (is an ) Instrument</p> <p>Here, Bass-guitar is the most specialized instrument category in the trail, and Instrument is the least specialized. Pseudo code to calculate the cumulative rank of an instrument in the inventory would be:</p> <p><i>var instrument:string = …; //</i>can take values shown in the switch, below</p> <p><i>var rank:uint = 0; </i></p> <p><i>var inst:uint = 1; </i></p> <p><i>var strinst:uint = 5; </i></p> <p><i>var guitar:uint = 10; </i></p> <p><i>var bassgtr:uint = 20; </i></p> <p><i>switch(instrument){ </i></p> <p><i>case 'instrument': </i></p> <p><i>rank += inst; </i></p> <p><i>case 'string-instrument': </i></p> <p><i>rank += strinst; </i></p> <p><i>case 'guitar': </i></p> <p><i>rank += guitar; </i></p> <p><i>case 'bass-guitar': </i></p> <p><i>rank += bassgui; </i></p> <p><i>default: </i></p> <p><i>trace('Cumulative rank of the instrument is : ' + rank); </i></p> <p><i>}</i></p> <p>In this context, a Guitar would have rank value equal to 16, while a bass-Guitar would have a rank value of 36. Please convince yourself of this output before you proceed further.</p> <p>Consider the following scenario: a special kind of ranking (via points allotted) is being maintained for products in a Red Light district shop. The ranking system classifies products in a hierarchy, and specialization is the aim. A more specialized product (under the classification hierarchy, which we will show in a moment) gets a cumulative-point-rank, meaning the sum of the ranks of each category it's part of, plus its own rank. For example, consider the following trail of classification:</p> <p>Rabbit (is) DILDO TOY (is) TOY (is) FUN PRODUCT (is) PRODUCT</p> <p>Here, the Rabbit's the most specialized product category in the trail: PRODUCT is the least specialized. Pseudo code to calculate the cumulative rank of a product in the inventory would be:</p> <p><i>var product:fun-product = …; //</i>can take values shown in the switch, below </p> <p><i>var rank:uint = 0; </i></p> <p><i>var prod:uint = 1; </i></p> <p><i>var fun-prod:uint = 5; </i></p> <p><i>var thetoy:uint = 10; </i></p> <p><i>var thedildo:uint = 20; </i></p> <p><i>var therabbit:uint = 30; </i></p> <p><i>switch(product){ </i></p> <p><i>case 'product': </i></p> <p><i>rank += prod; </i></p> <p><i>case 'fun-product': </i></p> <p><i>rank += fun-prod; </i></p> <p><i>case 'toy': </i></p> <p><i>rank += thetoy; </i></p> <p><i>case 'dildo-toy': </i></p> <p><i>rank += thedildo; </i></p> <p><i>case 'rabbit': </i></p> <p><i>rank += therabbit; </i></p> <p><i>default: </i></p> <p><i>trace('Cumulative rank of the product is : ' + rank);</i></p> <p><i>}</i></p> <p>Quick test: find the rank value of a toy, then that of a rabbit.</p> <p>If you found that the toy's rank value is 16, you get a cookie! If the rabbit has a rank value of 66, make that a chocolate chip cookie! Please be sure you understand how these values are obtained before going further.</p> <p>See how cleverly we have omitted break statements in all case statements, helping to compute the value for more specialized products by the cumulative addition of intermediary ranks. Also note that the ordering of the sequence of case statements is critical for this program to compute a product's correct cumulative rank!</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