views:

690

answers:

16

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

switch (someValue) {

  case OPTION_ONE:
  case OPTION_LIKE_ONE:
  case OPTION_ONE_SIMILAR:
    doSomeStuff1();
    break; // EXIT the switch

  case OPTION_TWO_WITH_PRE_ACTION:
    doPreActionStuff2();
    // the default is to CONTINUE to next case

  case OPTION_TWO:
    doSomeStuff2();
    break; // EXIT the switch

  case OPTION_THREE:
    doSomeStuff3();
    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
  case OPTION_ONE_SIMILAR:
    doSomeStuff1();
    // the default is to EXIT the switch

  case OPTION_TWO_WITH_PRE_ACTION:
    doPreActionStuff2();
    continue; // CONTINUE to next case

  case OPTION_TWO:
    doSomeStuff2();
    // the default is to EXIT the switch

  case OPTION_THREE:
    doSomeStuff3();
    // 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 ?

+3  A: 

PASCAL doesn't have fall-through

DVK
+1  A: 

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.

http://www.adaic.org/whyada/intro4.html

mdma
+1 for endogenous pointer sex
Marco Mariani
A: 

Tcl doesn't automatically fall through.

Bryan Oakley
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
A: 

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.

fuzzy lollipop
A: 

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:
  // default
  goto switch_end;
lab1:
  // do things
  goto switch_end; // if break is present
lab2:
  // do things, and follow to lab3
lab3:
  // lab3 stuffs
  goto switch_end;
//
...
switch_end:  // past all labS.
ShinTakezou
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
The construct you're trying to get as is a "jump table", and was a reasonably common trick in hand-tooled assembly coding.
dmckee
ShinTakezou
+1  A: 

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.

Oli
+5  A: 

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

Select Case i
    Case 1 to 3
        DoStuff(i)
    Case 4,5,6
        DoStuffDifferently(i)
    Case Is >= 7
        DoStuffDifferentlyRedux(i)
    Case Else
        DoStuffNegativeNumberOrZero(i)
End Select

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

Josh Smeaton
VB6 and it's predecessors were the same way.
Chris Kaminski
+8  A: 

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

object MatchTest2 extends Application {
  def matchTest(x: Any): Any = x match {
    case 1 => "one"
    case "two" => 2
    case y: Int => "scala.Int"
  }
  println(matchTest("two"))
}

Sample from scala-lang.org

Markust
Scala doesn't have fall-through statement, tho
Vitaly Polonetsky
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
@Vitaly, You can achieve the fallthrough behavior (sort of) in Scala like this: http://paste.pocoo.org/show/223917/
missingfaktor
@Rahul yeah, but you can't write functionCall() and then fall-through
Vitaly Polonetsky
@Vitaly, yes, that's why I said - sort of.
missingfaktor
Erlang has very similar Pattern matching
seanizer
same for Haskell, PLT Scheme, etc.
Claudiu
+1  A: 

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

seanizer
+4  A: 

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).

Vitaly Polonetsky
In Java the fallthrough is automatic if you omit the Break statement. This is the Cause of many programming errors.
seanizer
+9  A: 

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.

jweyrich
I believe the historical reason is that it was inherited from BCPL along with most of C's other syntax.
EJP
+3  A: 

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

Goyuix
+2  A: 

Hey, don't forget COBOL's EVALUATE:

EVALUATE MENU-INPUT
    WHEN "0" PERFORM INIT-PROC
    WHEN "1" THRU "9" PERFORM PROCESS-PROC
    WHEN "R" PERFORM READ-PARMS
    WHEN "X" PERFORM CLEANUP-PROC 
    WHEN OTHER PERFORM ERROR-PROC
END-EVALUATE.
ASalazar
Well, COBOL certainly PERFORMs.
Yishai
+1  A: 

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.

Warren P
+1  A: 

Pure speculation, but:

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

switch (tranCode)
{
  case 'A':
  case 'D':
  case 'R':
    processCredit();
    break;
  case 'B':
  case 'G':
    processDebit();
    break;
  default:
    processSpecial();
}

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.

Jay
It is a main reason for it. Other languages require a list of the alternatives after the keyword ('when' or whatever).
Jonathan Leffler
A: 

More languages without a fallthough:

XSLT JSTL Algol PL/1

EJP