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?

 
 
MaXiaochi@gmail.com
Guest
Posts: n/a
 
      04-26-2005
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.
In fact,I appreciate the counterpart "DO CASE ... ENDCASE" in
VFP,which takes not only equality but also other boolean expressions at
every case.If the expression is true, the coomands just after this case
excutes,ignoring other case,no matter they are true or not.I think it
much more convenient.
For example,to classify people by age,you code like this:
DO CASE
CASE age<3
kind=baby
CASE age<14
kind=child
CASE age<18
kind=adolescent
OTHERWISE
kind=adult
As to Java,the best way to achieve it is to use "if-else",only if we
divide people by 5,10,15,20, the "switch" is useful.

 
Reply With Quote
 
 
 
 
Betty
Guest
Posts: n/a
 
      04-26-2005

<> wrote in message
news: 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.
> In fact,I appreciate the counterpart "DO CASE ... ENDCASE" in
> VFP,which takes not only equality but also other boolean expressions at
> every case.If the expression is true, the coomands just after this case
> excutes,ignoring other case,no matter they are true or not.I think it
> much more convenient.
> For example,to classify people by age,you code like this:
> DO CASE
> CASE age<3
> kind=baby
> CASE age<14
> kind=child
> CASE age<18
> kind=adolescent
> OTHERWISE
> kind=adult
> As to Java,the best way to achieve it is to use "if-else",only if we
> divide people by 5,10,15,20, the "switch" is useful.


First of all, the java developers asked me if I wanted the break and
I said that I wanted it. Next, your example has no ENDCASE, so there.....


 
Reply With Quote
 
 
 
 
Steven
Guest
Posts: n/a
 
      04-26-2005
On 26 Apr 2005 11:14:20 -0700, ""
<> 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.
> In fact,I appreciate the counterpart "DO CASE ... ENDCASE" in
>VFP,which takes not only equality but also other boolean expressions at
>every case.If the expression is true, the coomands just after this case
>excutes,ignoring other case,no matter they are true or not.I think it
>much more convenient.
> For example,to classify people by age,you code like this:
>DO CASE
>CASE age<3
> kind=baby
>CASE age<14
> kind=child
>CASE age<18
> kind=adolescent
>OTHERWISE
> kind=adult
>As to Java,the best way to achieve it is to use "if-else",only if we
>divide people by 5,10,15,20, the "switch" is useful.


I cannot think of a way of coding this without using a nested if
statement, but if it is that important to you you could make your code
look like this:

package test;

public class CaseEnumTest {

/**
* @param args
*/
public static void main(String[] args) {
test(1);
test(5);
test(15);
test(25);
test(85);
test(200);
}

public static void test(int age)
{
switch(CaseEnum.isAge(age))
{
case BABY:
System.out.println("baby");
break;
case YOUTH:
System.out.println("youth");
break;
case TEEN:
System.out.println("teen");
break;
case ADULT:
System.out.println("adult");
break;
case SENIOR:
System.out.println("senior");
break;
default:
System.out.println("ok really old");

}
}

}

package test;

public enum CaseEnum {
NONE,
BABY,
YOUTH,
TEEN,
ADULT,
SENIOR
;

public static CaseEnum isAge(int age)
{
if(age <3) { return BABY; }
else if(age <13) { return YOUTH; }
else if(age <20) { return TEEN; }
else if(age <65) { return ADULT; }
else if(age <100) {return SENIOR; }
return NONE;
}
}

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

<> wrote in message
news: 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.


C and C++ compatibility.


 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      04-26-2005
<> schreef in bericht
news: 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;
}



 
Reply With Quote
 
Matthias Buelow
Guest
Posts: n/a
 
      04-26-2005
Mike Schilling <> 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;
}

Of course you could also use `if' statements with or-connected
predicates, or make a function/method with the desired result
expression and put it into each matching `case' clause (to avoid
duplicating code), or even simply textually duplicate the code (not
recommended), etc. It's just syntactical sugar. Unfortunately,
the unwanted side effect is that fall-through can happen, like in
C. Other languages like for example Lisp or Scheme have more
flexible constructs that prevent such errors and give the programmer
more choice. Unfortunately Java has decided to stick with the C
`switch' and also doesn't provide any way of creating new syntax.

mkb.
 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      04-27-2005
Know what you mean. I learned VB first, too.
We just gotta bite the bullet, and remember to throw those break stmts in
there


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

"Hal Rosser" <> wrote in message
newsCCbe.134716$ ...
> Know what you mean. I learned VB first, too.
> We just gotta bite the bullet, and remember to throw those break stmts in
> there
>

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
 
Oscar kind
Guest
Posts: n/a
 
      04-27-2005
Betty <> wrote:
> 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.


See the answers of Matthias and/or Boudewijn for that.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Reply With Quote
 
Ben Caradoc-Davies
Guest
Posts: n/a
 
      04-27-2005
Mike Schilling wrote:
> <> wrote in message
> news: 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. :->

--
Ben Caradoc-Davies <>
http://wintersun.org/
 
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