Take the 2-minute tour ×

It's the same syntax in a way too many languages:

switch (someValue) {

  case OPTION_ONE:
    break; // EXIT the switch

    // the default is to CONTINUE to next case

  case OPTION_TWO:
    break; // EXIT the switch

    break; // EXIT the switch


Now all you know that break statements are required, because the switch will continue to the next case when break statement is missing. We have an example of that with OPTION_LIKE_ONE, OPTION_ONE_SIMILAR and OPTION_TWO_WITH_PRE_ACTION. The problem is that we only need this "skip to next case" very very very rarely. And very often we put break at the end of case.

It very easy for a beginner to forget about it. And one of my C teachers even explained it to us as if it was a bug in C language (don't want to talk about it :)

I would like to ask if there are any other languages that I don't know of (or forgot about) that handle switch/case like this:

switch (someValue) {

  case OPTION_ONE:  continue; // CONTINUE to next case
  case OPTION_LIKE_ONE:  continue; // CONTINUE to next case
    // the default is to EXIT the switch

    continue; // CONTINUE to next case

  case OPTION_TWO:
    // the default is to EXIT the switch

    // the default is to EXIT the switch


The second question: is there any historical meaning to why it is like this in C? May be continue to next case was used far more often than we use it these days ?

share|improve this question
Not something I can get excited about, as a very rarely use it. In the project I'm working on, switch is used twice in 46 source files. –  anon Jun 10 '10 at 13:21
Every language has constructions that might lead to problems if not paying attention to what you write. Therefore, I always use PC-Lint since it finds these kinds of problems very easily. –  Patrick Jun 10 '10 at 13:24
Yes, it is a historical relic which is now a mis-feature. It was a Do not Repeat Yourself syntax when function calls were really expensive and every additional machine instruction took 2 days to execute (okay, PDP-11s weren't that slow, but if your mobile phone was as slow you'd shoot it). –  msw Jun 10 '10 at 13:25
@Neil: But only because it obviously sucks in the language you use. On the other hand, in languages such as Haskell or Scala, “switch” (well, actually case pattern matching) is such a powerful construct that it’s used all the time. –  Konrad Rudolph Jun 10 '10 at 14:07
@msw: without proof, I assert that it is likely the result of directly translating the jump-table flow control then common in assembly programming to structured languages. –  dmckee Jun 10 '10 at 14:08

17 Answers 17

up vote 11 down vote accepted

From this article, I can enumerate some languages that don't require a break-like statement:

  1. Ada (no fallthrough)
  2. Eiffel (no fallthrough)
  3. Pascal (no fallthrough)
  4. Go - fallthrough
  5. Perl - continue
  6. Ruby (no fallthrough)
  7. VB, VBA, VBS, VB.NET (no fallthrough)
  8. To be continued by someone else...

Your second question is pretty interesting. Assuming only C, I believe this decision keeps the language cohesive. Since break is a jump, it must be explicitly written.

share|improve this answer
I believe the historical reason is that it was inherited from BCPL along with most of C's other syntax. –  EJP Jun 12 '10 at 2:07

Scala pattern matching I think is a huge improvement in these cases. :)

object MatchTest2 extends Application {
    case 1 => "one"
    case "two" => 2
    case y: Int => "scala.Int"

Sample from scala-lang.org

share|improve this answer
Scala doesn't have fall-through statement, tho –  Vitaly Polonetsky Jun 10 '10 at 13:36
Yep, absolutely right but maybe is because it does not require one :). Scala is a functional and OO language, something that C is not, maybe that's why lacks of a fall-through. Anyway, I'm not an expert in Scala by all means . –  Markust Jun 10 '10 at 13:44
@Vitaly, You can achieve the fallthrough behavior (sort of) in Scala like this: paste.pocoo.org/show/223917 –  missingfaktor Jun 10 '10 at 13:48
@Rahul yeah, but you can't write functionCall() and then fall-through –  Vitaly Polonetsky Jun 10 '10 at 13:59
same for Haskell, PLT Scheme, etc. –  Claudiu Jun 10 '10 at 20:27

And VB .NET handles it a little more like how you expect it should work.

Select Case i
    Case 1 to 3
    Case 4,5,6
    Case Is >= 7
    Case Else
End Select

There is no fall through at all, without possibly using a Goto

share|improve this answer
VB6 and it's predecessors were the same way. –  Chris Kaminski Jun 10 '10 at 13:56

Here is the answer: http://en.wikipedia.org/wiki/Switch_statement

It's called fall-through statement (continue in the example) and it exists in the following languages:
Go, Perl, C#

In C# it won't compile without break or goto case statement (except when there's no pre-action).

share|improve this answer
In Java the fallthrough is automatic if you omit the Break statement. This is the Cause of many programming errors. –  Sean Patrick Floyd Jun 10 '10 at 15:34

PASCAL doesn't have fall-through

share|improve this answer

I think the answer to your question of why it is this way centers around two behaviors, both having to do with the generated assembly code from the C source.

The first is that in assembly, the current instruction is executed, and unless there is a jump or some other flow control instruction, the instruction at the next address will be executed. Performing a naive compile of the switch statement to assembly would generate code that would just start executing the first instruction, which would be to see if there was a matching condition...

The second related reason is the notion of a branch table or jump list. Basically the compiler can take what it knows about your value, and create some extremely efficient machine code for the same thing. Take for example a simple function like atoi that converts a string representation of a number and returns it in integer form. Simplifying things way down to support just a single digit, you could write some code similar to this:

int atoi(char c) {
  switch (c) {
    case '0': return 0;
    case '1': return 1;
    // ....

The naive compiler would perhaps just convert that to a series of if/then blocks, meaning a substantial amount of CPU cycles would be taken for the number 9, while 0 returns almost immediately. Using a branch table the compiler could emit some [psuedo] assembly that would immediately "jump" to the correct return clause:

0x1000 # stick value of c in a register
0x1004 # jump to address c + calculated offset
# example '0' would be 0x30, the offset in for this sample
# would always be 0x0FD8... thus 0x30 + 0x0FD8 = 0x1008
0x1008 # return 0 

Apology: my assembly and C skills are quite rusty. I hope this helps clarify things. 0x

share|improve this answer

Hey, don't forget COBOL's EVALUATE:

share|improve this answer
Well, COBOL certainly PERFORMs. –  Yishai Jun 10 '10 at 16:16

Ada doesn't have fallthrough, and requires that all values are explicitly handled, or a "others" clause added to handle the rest.

SQL CASE statement also does not fallthrough. XSLT has which does not fallthrough.

It seems to be C and derived languages that have the fallthrough. It's quite insidious, and the only real use I've seen is implementing duff's device.


share|improve this answer
+1 for endogenous pointer sex –  Marco Mariani Jun 11 '10 at 13:26

Python doesn't have one at all.

Took some getting used to but I have some horrendous memories hunting through massive switch blocks back in my C# days. I'm much happier without it.

share|improve this answer

Although not exactly what you asked for, Groovy has a very powerful switch statement

share|improve this answer

The OP talks about "fall through", but very seldom have I ever been bit by that.

Many many times, however I have been bit by designs that are non-extensible. To wit, "switch (kbHit)" statements, with a few hundred keys in there, that are a maintenance nightmare, and a frequent location for "god methods", and giant piles of spaghetti-code.

Using switch is often a sign of poor object oriented programming. As another person answered, "2 uses of Switch in 48 source files", in one of his application, shows a programmer who does not rely heavily on this construct. From his metric, I surmise that he is probably at least a good structured programmer, and probably understands OOP/OOD as well.

OOP (not necessarily only C++) programmers, and even pure C users who do not have an object description technique forced upon them, could implement an "inversion of control" container that publishes a "key was hit" and allows subscribers to plug in their handlers for "on keyboard code x". This can make reading your code much easier.

share|improve this answer

Pure speculation, but:

I occasionally write C or Java in which I say something like:

switch (tranCode)
  case 'A':
  case 'D':
  case 'R':
  case 'B':
  case 'G':

That is, I deliberately use fall-thru to let several values fire the same operation.

I wonder if this is what the inventors of C were thinking of when they created the SWITCH statement, that this would be the normal usage.

share|improve this answer
It is a main reason for it. Other languages require a list of the alternatives after the keyword ('when' or whatever). –  Jonathan Leffler Jun 11 '10 at 4:44

Tcl doesn't automatically fall through.

share|improve this answer
But it does allow multiple matches to chain together so as to use the same body. switch $thing { a - b - c { puts "was a,b or c" } d - e { puts "was d or e" } default { puts "something else" } } –  Donal Fellows Jun 10 '10 at 13:37

In object oriented languages you use the Chain of Responsibility pattern. How that is implemented can vary. What you are describing in your example, is mixing a state machine with behavior by abusing a switch statement. For your particular example a Chain of Responsibility pattern with the parameter that the chain evaluates being a State pattern, that mutates as it goes down the chain, would be the appropriate implementation.

share|improve this answer

languages are too much and I can't answer for sure that there's not such a language, provided it is a "derivative" of C in syntax, because other languages using different syntax and where the case does not "continue" naturally exist, e.g. Fortran. I don't know languages that uses an explicit "continue" to continue to the following case.

I believe it is historical reason due to the way such a case could be programmed at a "low level". Moreover the syntactical aspect of the case is that of a label, and break works like in loops, so you can imagine an equivalent like this:

if ( case == 1 ) goto lab1;
if ( case == 2 ) goto lab2;
if ( case == 3 ) goto lab3;    
  // default
  goto switch_end;
  // do things
  goto switch_end; // if break is present
  // do things, and follow to lab3
  // lab3 stuffs
  goto switch_end;
switch_end:  // past all labS.
share|improve this answer
As you can see, in this case it is the "break" to add code, while "doing nothing" and continuing to the next label is one instruction less and what "naturally" happens if not said otherwise (through the break) –  ShinTakezou Jun 10 '10 at 13:34
The construct you're trying to get as is a "jump table", and was a reasonably common trick in hand-tooled assembly coding. –  dmckee Jun 10 '10 at 14:10
yes ok. indeed it is a HL coding of a possible implementation in assembly, where the if(..) goto ... part can be optimized into a jump table, wherever possible. And IMO it explains quite well why K&R's switch needs a break to delimit cases, and why case syntax resembles labels' definitions –  ShinTakezou Jun 10 '10 at 14:25

More languages without a fallthough:


share|improve this answer

Lima does it like this:

if someValue == ?1 ::
   OPTION_ONE: fall
     ;; the default is to EXIT the switch

     fall                 ;; fall through to the next case


share|improve this answer

Your Answer


