Re: Control cannot fall through from one case label ('case 115:')



Nikola Novak wrote:
On Thu, 5 Feb 2009 17:21:24 -0600, Ben Voigt [C++ MVP] wrote:

Nikola Novak wrote:
On Wed, 4 Feb 2009 12:47:06 +0100, Rudy Velthuis wrote:

Peter Morris wrote:

They could have done it the Pascal way. No problems with
fall-through or common typo bugs there. <g>

Yes they could have. But then again, having to mangle your source
because you can't do a switch on a string wouldn't have been fun
would it?

They could have done that for strings as well, no biggie. <g>

I was thinking of something along the lines of

switch (bla)
{
case 1, 3, 5:
code ...
break;
case 2, 4, 9:
code ...
break;
case 10..30:
code ...
break;
default:
code ...
break;
}

Actually, the more interesting thing would be this:

switch(blah)
{
case 1:
// code
case 2:
// more code
case 3:
// even more code
default:
// still more code!
break;
}

Now it takes gotos and stuff to make it work and it doesn't do much
for the code tidyness. Of course, let's not forget the Duff's
device... a thing of beauty...

but the cases are themselves labels which you can jump to with goto.
Although the code is longer, it is clearer which I think is the prime
requisite for "tidyness".

And Duff's device is great in a few limited circumstances when
properly explained and encapsulated. I've started to think that
usually a "goto" would be preferable (at least in cases where the
first iteration doesn't start at a number of different lines
depending on a single variable).

I think that the fallthrough concept is clearer than jumping to the
next line with "goto".

It's more concise, but definitely not clearer. In C (or C++) the only
indication that execution continues with the next block is the *absence* of
a branch construct (such as break or return) and since 99+% of cases do end
with a branch, my brain tends to add the missing "branch" while you're
reading the code.

Whereas with explicit goto the sequence of execution is abundantly clear. I
suppose there could have been a "fallthrough" keyword equivalent to "goto
case <whatever-comes-next>" or even a designator on the switch block as a
whole to permit silent fallthrough. But I definitely prefer the C# decision
to require the programmer to explicitly request fallthrough -- it's used so
infrequently that a case block without a branch construct is a bug more
often than not.


Besides, the whole purpose of Duff's device is to make the first
iteration start at any one of the lines depending on a single
variable. If you wanted to do that with goto instead of switch, you'd
need a really ugly if ... else if ... else if ... block, or another
switch which is quite pointless because you can just use the Duff's
device then.

Duff's device also allows starting in the middle of the loop on the first
iteration, which is more efficient execution-wise than mid-loop exit
conditions. But so would a plain goto from outside the loop, or a compiler
smart enough to reorder the halves of the loop to eliminate the
unconditional branch at the bottom.


The only reason I mentioned Duff's device here though, is because its
elegance (as an optimized solution) is obliterated by the use of
gotos on each line.

Duff's device isn't possible in C# at all AFAIK, you can't have looping
constructs stretching across multiple cases.


Nikola


.



Relevant Pages


Loading