Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » Conditionals 2: 'switch' » Switch In Action: Days Of The Month
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

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)

Switch In Action: Days Of The Month

As we have seen in the previous summary, the 'break' keyword plays a very significant role on the flow of program control inside a switch construct. 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 "fall-through" to the next case, and the next and so on...

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.

In fact, we'll now discuss an example where using an if-conditional is very cumbersome, and like in the previous summary, revolves around condition testing of a single variable. We'll also see that even when an if-construct is structured differently in this context, the switch still wins hands-down.

Consider the following scenario:

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 if-else construct would be something like:

var num:int = …;

if(num == 2 || num == 3 || num == 5 || num == 7){

trace(num + "is primen");

}else if(num == 4 || num == 6 || num == 8 || num == 9 || num == 10){

trace(num + "is not primen");

}else{

trace(num + "is God only knows whatn");

}

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 previous summary, a switch can enhance the readability of code involving the testing of a single identifier. Check this out:

switch(num){

case 2:

case 3:

case 5:

case 7: trace(num + "is primen");

break;

case 4:

case 6:

case 8:

case 9:

case 10: trace(num + "is not primen");

break;

default:

trace(num + "is God only knows what");

}

Look how cleverly we have used the explicit-exit-mechanism feature (the break) of the switch 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.

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.
Act now