Forgot username/password? Don't have an account?
Login with Facebook
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

Introduction to the syntax and context of applicability of the switch construct

Now let's examine the switch construct more closely, and also compare it with the intuitive and popular if-else-if-else construct. If you recall the conditionals example in the conditionals-2 course, we cited a scenario where a person had to travel from Brooklyn Bridge to the Statue of Liberty by bus, and wanted to know how to make use of the change available. The pseudo-code illustrating their decision-making appeared something like:

if(denomination == "quarters"){ Change = 2Q; }else if(denomination == "dimes"){ Change = 5D; }else if(denomination == "nickels"){ Change = 10N; }else if(denomination == "pennies"){ Change = 50P; }else{ assWalk = true; }

So here we have a case where a single identifier / variable which has some content, is being successively compared to several values in a cascading if-else-else-if construct. One has the chance to get smart here, and the language designers understand this too. Now don't get me wrong: there is no logical error in this piece of code whatsoever. It's perfectly fine. But correctness in logic is not the only criteria for judging software.

Often software is developed by a team of architects, managers, developers, testers and deployment engineers. Frequently, code that's written has to be reviewed by others; it also turns out that initial developers might not be the ones maintaining or enhancing it. People come and go; that's how life is... we cannot change that!

We were talking about the switch construct. The only problem with the above code chunk is that it's a bit of an eyesore! There is a lot of cluttering, what with repeated usage of the variable name, multiple explicit comparisons, and what not!

The switch construct comes in handy here. The syntax of a switch statement is:

switch(condition) { //blah blah blah }

We could rewrite our change denomination example as:

switch(denomination) { case 'Q' : change = 2Q; break; case 'D' : change = 5D; break; case 'N' : change = 10 N; break; case 'P' : change = 50 P; break; default : assWalk = true; }

Aah! Looks much much cleaner to read and understand, doesn't it? That's the advantage of having a language construct like the switch in certain contexts. I know, there are a few points that are still not clear; yes, the 'break' keyword! What is it doing here? Don't worry, we'll find out very soon, in the next summary.

A point that deserves mention is the 'default' keyword : it represents the same logical construct as the final 'else ' in an 'if-else if-else' cascading construct, which we covered in the conditionals 1 course. The code above explicitly displays the relationship between the two styles of programming.

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