Learn Create Your Own Split Screen

Next Page

But first, hopefully you looked at that previous example and snorted with derision when you saw that last break statement.
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; }

The purpose of break is to stop any subsequent cases from executing, but there are no other statements after case 3, so that break-statement is completely unnecessary.

Yes it is. But it's also a good habit to get into. Months later, you might be changing your program and add some more cases - say case 4 and case 5. But unless you remember to go add the break into case 3, your case 3 will fallthrough to case 4 (but none of the others will), creating a very confusing glitch.

It's good programming practice to always finish each case with a break, unless you really do want it to fallthrough. And that's exactly what we will in the next example.