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

Breaking The switch Down

<h2>The importance of using the 'break' keyword in switch constructs</h2> <p>We trust you have understood the significance of the <a href="/courses/video/9/70/Switch.html">switch</a> construct from the preceding summary. You would surely have noticed that the only keyword in the switch that does not have a peer in the if-else-if-else construct is the 'break' keyword. We never used such a keyword in the if cascades because the language standard took care of the comparison-execute-exit mechanism all by itself.</p> <p>The language does not guarantee by itself the third part, that's, the exit mechanism. This is the stand-out difference between the two conditional constructs. The programmer is expected to take control of the exit mechanism by using the 'break' keyword. We can illustrate this more succinctly by looking at an example. Consider the following scenario:</p> <p>A model is applying for work at a modeling agency and wants to become the next supermodel. The application form has a field called 'Sex' where the model has to enter the relevant entry from the choices {'M', 'F', 'B'}... Male, Female, Both.</p> <p>Pseudo-code of a program that scans applications for registration would look like:</p> <p><i>switch(sex) </i></p> <p><i>{</i></p> <p><i>case 'M' : trace(&quot;Male&quot;); </i> </p> <p><i>case 'F' : trace(&quot;Female&quot;); </i> </p> <p><i>default: trace(&quot;Transsexual&quot;); </i></p> <p><i>}</i></p> <p>What would the output be if the value entered by the model was 'M' (male)?Let's cut the crap and go straight to the heart of the matter. The output of the above program, when run, turns out to be:</p> <b> Male Female Transsexual</b> <p>Surprising, isn't it? Even though the model correctly entered his sex to be 'M', the computer program we wrote forced him to be all 3 choices. That does not normally happen in the real world, though. This issue has everything to do with the exit mechanism of switch, which has to be taken care of by the programmer. Watch carefully... the correct way to write code for the scanner program would be:</p> <p><i>switch(sex) </i></p> <p><i>{</i></p> <p><i>case 'M' : trace(&quot;Male&quot;); </i></p> <p><i>break; </i></p> <p><i>case 'F' : trace(&quot;Female&quot;); </i></p> <p><i>break; </i></p> <p><i>default: trace(&quot;Transsexual&quot;); </i></p> <p><i>break; </i></p> <p><i>}</i></p> <p>Now, the program will correctly print 'M' for the same model. Got the hang of it? This is the only catch with the switch construct. Now, we would like you to predict the output of the following code chunk if sex=&quot;M&quot; (there might be cookies involved!):</p> <p><i>switch(sex) </i></p> <p><i>{</i></p> <p><i>case 'M' : trace(&quot;Male&quot;); </i></p> <p><i>case 'F' : trace(&quot;Female&quot;); </i></p> <p><i>break; </i></p> <p><i>default: trace(&quot;Transsexual&quot;); </i></p> <p><i>}</i></p> <p>If you correctly guessed that the output would be:</p> <b>Male Female</b> <p>YOU get a cookie! Got it? Great! You are now an oracle on switch behavior!</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