Home » Courses » Conditionals 2: 'switch' » What if Can't Do...

### 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)

### What if Can't Do...

We'll now look at an example which can only be elegantly written using a switch construct. It's our usual unusual example, which generally appeals to the nut within!

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:

Bass-Guitar (is a) Guitar (is a) Stringed-Instrument (is an ) Instrument

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:

var instrument:string = …; //can take values shown in the switch, below

var rank:uint = 0;

var inst:uint = 1;

var strinst:uint = 5;

var guitar:uint = 10;

var bassgtr:uint = 20;

switch(instrument){

case 'instrument':

rank += inst;

case 'string-instrument':

rank += strinst;

case 'guitar':

rank += guitar;

case 'bass-guitar':

rank += bassgui;

default:

trace('Cumulative rank of the instrument is : ' + rank);

}

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.

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:

Rabbit (is) DILDO TOY (is) TOY (is) FUN PRODUCT (is) PRODUCT

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:

var product:fun-product = …; //can take values shown in the switch, below

var rank:uint = 0;

var prod:uint = 1;

var fun-prod:uint = 5;

var thetoy:uint = 10;

var thedildo:uint = 20;

var therabbit:uint = 30;

switch(product){

case 'product':

rank += prod;

case 'fun-product':

rank += fun-prod;

case 'toy':

rank += thetoy;

case 'dildo-toy':

rank += thedildo;

case 'rabbit':

rank += therabbit;

default:

trace('Cumulative rank of the product is : ' + rank);

}

Quick test: find the rank value of a toy, then that of a rabbit.

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.

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!

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.