Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why doesn't "switch" statement have "break" as default?

Reply
Thread Tools

Why doesn't "switch" statement have "break" as default?

 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      04-27-2005
Ben Caradoc-Davies <(E-Mail Removed)> writes:

> "Many people (even bwk?) have said that the worst feature of C is that
> switches don't break automatically before each case label.


Should be continued: "Most of these come from a Pascal/Modula background."



C# "fixes" this by making it an error not to have a break or goto
<other label> at the end of each labeled section.
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      04-27-2005
Ben Caradoc-Davies wrote:

> Mike Schilling wrote:
>
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>
>> C and C++ compatibility.

>
>
> Which brings us to "Duff's device":
>
> http://www.jargon.net/jargonfile/d/Duffsdevice.html
> http://www.lysator.liu.se/c/duffs-device.html
>
> "Many people (even bwk?) have said that the worst feature of C is that
> switches don't break automatically before each case label. This code
> forms some sort of argument in that debate, but I'm not sure whether
> it's for or against."
>
> I have not dared to see if Duff's Device works in Java. :->


It does not. The Java version of the switch statement is more
structured than the C version: if the innermost block containing a case
statement is not the one introduced by a switch statement then the
compiler rejects the code. In particular:


public class Duff {
int i;
int[] j;

public void duff(int count) {
int n = (count + 7) / 8; /* count > 0 assumed */
int ctr = 0;
switch (count %
{
case 0: do { i += j[ctr++];
case 7: i += j[ctr++];
case 6: i += j[ctr++];
case 5: i += j[ctr++];
case 4: i += j[ctr++];
case 3: i += j[ctr++];
case 2: i += j[ctr++];
case 1: i += j[ctr++];
} while (--n > 0);
}
}
}


D:\temp\testdir>javac Duff.java
Duff.java:12: orphaned case
case 7: i += j[ctr++];
^
Duff.java:13: orphaned case
case 6: i += j[ctr++];
^
Duff.java:14: orphaned case
case 5: i += j[ctr++];
^
Duff.java:15: orphaned case
case 4: i += j[ctr++];
^
Duff.java:16: orphaned case
case 3: i += j[ctr++];
^
Duff.java:17: orphaned case
case 2: i += j[ctr++];
^
Duff.java:18: orphaned case
case 1: i += j[ctr++];
^
7 errors

(That version of the Duff device adapted from the C code in the Jargon
file.)

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Thomas Schodt
Guest
Posts: n/a
 
      04-27-2005
Ben Caradoc-Davies wrote:

> I have not dared to see if Duff's Device works in Java. :->


It does not.

<http://www.preemptive.com/documentation/NewObfuscation.html>

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      04-27-2005
On Wed, 2005-04-27 at 00:36 +0200, Boudewijn Dijkstra wrote:
> <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed) oups.com...
> > In most of the case,there is a "break" after every "case" in switch
> > statement, poor coders have to write "break" explicitly again and
> > again,and the worse is that sometimes someone forget to do that.So why
> > don't the designers make it a default state? if sometimes someone
> > prefer his switch statement without a break,he can achieve it by
> > duplicating code for once.

>
> This is why:
>
> switch (type)
> {
> case COMMAND_RC5:
> case COMMAND_RC5_STRING:
> case COMMAND_RC6_MODE_0:
> buf = new byte[4];
> buf[0] = info;
> contentIndex = 1;
> break;
> case COMMAND_RC5_EXTENDED:
> case COMMAND_CDI:
> case COMMAND_RC6_MODE_2A:
> buf = new byte[5];
> buf[0] = info;
> contentIndex = 1;
> break;
> case COMMAND_RC6_MODE_1A:
> case COMMAND_RC6_MODE_5:
> case COMMAND_RC6_MODE_6:
> buf = new byte[3 + content.length];
> buf[0] = info;
> buf[1] = (byte) (1 + content.length);
> contentIndex = 1;
> break;
> case COMMAND_RC6_MODE_1B:
> buf = new byte[3 + content.length];
> buf[0] = info;
> buf[1] = (byte) (content.length >> ;
> buf[2] = (byte) content.length;
> contentIndex = 3;
> break;
> default:
> return null;
> }
>
>
>


Couldn't have put it better myself.

--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + (E-Mail Removed)in


 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      04-27-2005
AH - "break" ain't mandatory for every case in a switch
but if you leave it off
you oughta have a reason you leave it off
else
live with fall-thru



> If the 'break' is mandatory, how do you have the same code for
> more than one switch value? And I don't mean in the 'default'
> statement, because that trick can only be used once. There is no
> goto remember.
>
>



 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      04-27-2005

"Matthias Buelow" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Mike Schilling <(E-Mail Removed)> wrote:
>
>>> In most of the case,there is a "break" after every "case" in switch
>>> statement, poor coders have to write "break" explicitly again and
>>> again,and the worse is that sometimes someone forget to do that.So why
>>> don't the designers make it a default state? if sometimes someone
>>> prefer his switch statement without a break,he can achieve it by
>>> duplicating code for once.

>>C and C++ compatibility.

>
> And simply because you might want to match the object against more
> than one keys, for example:
>
> switch (n) {
> case 2: case 3: case 5: case 7: case 11:
> return prime;
> case 1: case 4: case 6: case 8: case 9: case 10:
> return nonprime;
> }


That's a slightly different issue; a language could certainly define that a
block can have multiple labels but there is no fall-through between blocks.


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      04-28-2005
Ben Caradoc-Davies coughed up:
> Mike Schilling wrote:
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>>
>> C and C++ compatibility.

>
> Which brings us to "Duff's device":
>
> http://www.jargon.net/jargonfile/d/Duffsdevice.html
> http://www.lysator.liu.se/c/duffs-device.html
>
> "Many people (even bwk?) have said that the worst feature of C is that
> switches don't break automatically before each case label.



Far more than silly fall-throughs, which don't bug me that much, I
personally hate (without offering an alternative, so don't even ask) the
following:

long *a = 0; // assign 0 to the ptr

*a = 0; // assign 0 to the contents of the ptr

It is something that we all (probably) take as easy, but think in terms of a
newbie, and it is horribly counter-intuitive.

I also absolutely LOATH this:

long* a, b; // long pointer followed by a long







> This code
> forms some sort of argument in that debate, but I'm not sure whether
> it's for or against."
>
> I have not dared to see if Duff's Device works in Java. :->




--
Unix users who vehemently argue that the "ln" command has its arguments
reversed do not understand much about the design of the utilities. "ln
arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
Existing file argument to non-existing argument. And in fact, mv
itself is implemented as a link followed by an unlink.


 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      04-28-2005
"Hal Rosser" <(E-Mail Removed)> wrote in message
news:OQSbe.7232$(E-Mail Removed)...
> AH - "break" ain't mandatory for every case in a switch
> but if you leave it off
> you oughta have a reason you leave it off
> else
> live with fall-thru


If I intentionally allow fall-through, I usually document it so another
developer (or myself 6 months later) doesn't look at the code and assume
it's a mistake:

switch (whatever)
{
case 0:
// don't break; allow fall-through
case 1:
break;
}

--
Grant Wagner <(E-Mail Removed)>


 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      04-28-2005
(E-Mail Removed) <(E-Mail Removed)> wrote or quoted:

> In most of the case,there is a "break" after every "case" in switch
> statement, poor coders have to write "break" explicitly again and
> again, and the worse is that sometimes someone forget to do that.
> So why don't the designers make it a default state?


That's easy:

Java 5? Backwards compatibility with Java 1.0;
Java 1.0? Backwards compatibility C;
C? The designers didn't know any better.
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      04-28-2005
Ben Caradoc-Davies <(E-Mail Removed)> wrote or quoted:

[Duff's device]

> http://www.lysator.liu.se/c/duffs-device.html


ISTM that this bit:

``Actually, I have another revolting way to use switches to implement
interrupt driven state machines but it's too horrid to go into.

....is reminiscent of Fermat's Last Theorem
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
unless statement... why oh why? Zach Dennis Ruby 6 08-18-2005 11:56 PM



Advertisments