Learn Create Your Own Split Screen

Next Page

Consider this code fragment:
var s = ""; var x = 2; switch (x) { case 1: s = s + " one "; break; case 2: s = s + " two "; break; case 3: s = s + " three "; break; }

After that code runs, the value of s will be " two ". No surprises.

But without the break statements:

var s = ""; var x = 2; switch (x) { case 1: s = s + " one "; case 2: s = s + " two "; case 3: s = s + " three "; }

The value of s will be " two three ". Keep looking at those two examples and be sure you can see the difference.


How stoopid and inconvenient, right?

Well, sometimes. That's why some languages have fallthrough and some don't. Let's look at a situation where fallthrough becomes quite convenient.