Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Why doesn't "switch" statement have "break" as default? (http://www.velocityreviews.com/forums/t143045-why-doesnt-switch-statement-have-break-as-default.html)

MaXiaochi@gmail.com 04-26-2005 06:14 PM

Why doesn't "switch" statement have "break" as default?
 
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.


Betty 04-26-2005 06:30 PM

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

<MaXiaochi@gmail.com> wrote in message
news:1114539260.597250.223020@f14g2000cwb.googlegr 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.....



Steven 04-26-2005 07:02 PM

Re: Why doesn't "switch" statement have "break" as default?
 
On 26 Apr 2005 11:14:20 -0700, "MaXiaochi@gmail.com"
<MaXiaochi@gmail.com> 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

Mike Schilling 04-26-2005 10:10 PM

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

<MaXiaochi@gmail.com> wrote in message
news:1114539260.597250.223020@f14g2000cwb.googlegr 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.



Boudewijn Dijkstra 04-26-2005 10:36 PM

Re: Why doesn't "switch" statement have "break" as default?
 
<MaXiaochi@gmail.com> schreef in bericht
news:1114539260.597250.223020@f14g2000cwb.googlegr 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 >> 8);
buf[2] = (byte) content.length;
contentIndex = 3;
break;
default:
return null;
}




Matthias Buelow 04-26-2005 10:49 PM

Re: Why doesn't "switch" statement have "break" as default?
 
Mike Schilling <mscottschilling@hotmail.com> 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.

Hal Rosser 04-27-2005 02:19 AM

Re: Why doesn't "switch" statement have "break" as default?
 
Know what you mean. I learned VB first, too.
We just gotta bite the bullet, and remember to throw those break stmts in
there



Betty 04-27-2005 02:58 AM

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

"Hal Rosser" <hmrosser@bellsouth.net> wrote in message
news:DCCbe.134716$vL3.84495@bignews4.bellsouth.net ...
> 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.



Oscar kind 04-27-2005 05:17 AM

Re: Why doesn't "switch" statement have "break" as default?
 
Betty <lisa@nospam.invalid> 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

Ben Caradoc-Davies 04-27-2005 12:21 PM

Re: Why doesn't "switch" statement have "break" as default?
 
Mike Schilling wrote:
> <MaXiaochi@gmail.com> wrote in message
> news:1114539260.597250.223020@f14g2000cwb.googlegr 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 <ben@wintersun.org>
http://wintersun.org/


All times are GMT. The time now is 10:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.