Connecting Tech Pros Worldwide Forums | Help | Site Map

switch-statement ???

Chris
Guest
 
Posts: n/a
#1: Nov 16 '05
Hi,

can you specify a range in a switch - statement ?

switch (i)
{
case 100 - 999 :
// do something
break;
case 1000 - 9999:
// do something else
break;
}

Previous syntax is not allowed, I know,
but can you solve this using 'switch' ? without having to provide a case
statement for every possible value and without using 'if' ?

thanks
Christian




Michael C
Guest
 
Posts: n/a
#2: Nov 16 '05

re: switch-statement ???


Hi Chris,

We were discussing the heck out of that a few months ago, and the answer is
(in C#), no. On the other hand, if you only have two ranges as in the
example you posted, an if...else construct would be almost as efficient. If
you have only a couple of options per case statement, case can drop-through
like this:

switch (i)
{
case 100:
case 101:
// do something
break;
case 1000:
case 1001:
case 1002:
// do something else
break;
}

Thanks,
Michael C., MCDBA

"Chris" <christianc@pandora.be> wrote in message
news:o2V9d.273844$rh2.14058566@phobos.telenet-ops.be...[color=blue]
> Hi,
>
> can you specify a range in a switch - statement ?
>
> switch (i)
> {
> case 100 - 999 :
> // do something
> break;
> case 1000 - 9999:
> // do something else
> break;
> }
>
> Previous syntax is not allowed, I know,
> but can you solve this using 'switch' ? without having to provide a case
> statement for every possible value and without using 'if' ?
>
> thanks
> Christian
>
>
>[/color]


Paul E Collins
Guest
 
Posts: n/a
#3: Nov 16 '05

re: switch-statement ???


"Chris" <christianc@pandora.be> wrote:
[color=blue]
> switch (i)
> {
> case 100 - 999 :[/color]

That will actually compile, but it won't do what you want. (The minus
sign is subtraction, so it's equivalent to "case -899".)

You can't specify ranges in a C# switch statement.

P.


JuLiE Dxer
Guest
 
Posts: n/a
#4: Nov 16 '05

re: switch-statement ???


I thought fall-throughs in switch statements were not supported in C#
(design choice).

At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
by Tom Archer

On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <michaelc@nospam.org>
wrote:
[color=blue]
>Hi Chris,
>
>We were discussing the heck out of that a few months ago, and the answer is
>(in C#), no. On the other hand, if you only have two ranges as in the
>example you posted, an if...else construct would be almost as efficient. If
>you have only a couple of options per case statement, case can drop-through
>like this:
>
>switch (i)
>{
> case 100:
> case 101:
> // do something
> break;
> case 1000:
> case 1001:
> case 1002:
> // do something else
> break;
>}
>
>Thanks,
>Michael C., MCDBA
>
>"Chris" <christianc@pandora.be> wrote in message
>news:o2V9d.273844$rh2.14058566@phobos.telenet-ops.be...[color=green]
>> Hi,
>>
>> can you specify a range in a switch - statement ?
>>
>> switch (i)
>> {
>> case 100 - 999 :
>> // do something
>> break;
>> case 1000 - 9999:
>> // do something else
>> break;
>> }
>>
>> Previous syntax is not allowed, I know,
>> but can you solve this using 'switch' ? without having to provide a case
>> statement for every possible value and without using 'if' ?
>>
>> thanks
>> Christian
>>
>>
>>[/color]
>[/color]

Eran Kampf
Guest
 
Posts: n/a
#5: Nov 16 '05

re: switch-statement ???


From MSDN:

Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something;

Eran Kampf
http://www.ekampf.com

"JuLiE Dxer" <MsJuLiE@verizon.net> wrote in message
news:ub4im0dl7ckq8ofju6sd5e57aiq4dme9cs@4ax.com...[color=blue]
>I thought fall-throughs in switch statements were not supported in C#
> (design choice).
>
> At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
> by Tom Archer
>
> On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <michaelc@nospam.org>
> wrote:
>[color=green]
>>Hi Chris,
>>
>>We were discussing the heck out of that a few months ago, and the answer
>>is
>>(in C#), no. On the other hand, if you only have two ranges as in the
>>example you posted, an if...else construct would be almost as efficient.
>>If
>>you have only a couple of options per case statement, case can
>>drop-through
>>like this:
>>
>>switch (i)
>>{
>> case 100:
>> case 101:
>> // do something
>> break;
>> case 1000:
>> case 1001:
>> case 1002:
>> // do something else
>> break;
>>}
>>
>>Thanks,
>>Michael C., MCDBA
>>
>>"Chris" <christianc@pandora.be> wrote in message
>>news:o2V9d.273844$rh2.14058566@phobos.telenet-ops.be...[color=darkred]
>>> Hi,
>>>
>>> can you specify a range in a switch - statement ?
>>>
>>> switch (i)
>>> {
>>> case 100 - 999 :
>>> // do something
>>> break;
>>> case 1000 - 9999:
>>> // do something else
>>> break;
>>> }
>>>
>>> Previous syntax is not allowed, I know,
>>> but can you solve this using 'switch' ? without having to provide a case
>>> statement for every possible value and without using 'if' ?
>>>
>>> thanks
>>> Christian
>>>
>>>
>>>[/color]
>>[/color]
>[/color]


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: switch-statement ???


Also you are aloud to transfer control from from case block to another (which gives the same functionality as fall-through, but in an obviously inteded way rather than happning by accident.

switch(i)
{
case 0:
case 1:
// do some stuff
goto case 2;
case 2:
// do some other stuff
break;
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<uL0rv2rrEHA.3488@TK2MSFTNGP11.phx.gbl>

From MSDN:

Although fall through from one case label to another is not supported, it is
allowed to stack case labels, for example:
case 0:
case 1:
// do something;

Eran Kampf
http://www.ekampf.com

"JuLiE Dxer" <MsJuLiE@verizon.net> wrote in message
news:ub4im0dl7ckq8ofju6sd5e57aiq4dme9cs@4ax.com...[color=blue]
>I thought fall-throughs in switch statements were not supported in C#
> (design choice).
>
> At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
> by Tom Archer
>
> On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <michaelc@nospam.org>
> wrote:
>[color=green]
>>Hi Chris,
>>
>>We were discussing the heck out of that a few months ago, and the answer
>>is
>>(in C#), no. On the other hand, if you only have two ranges as in the
>>example you posted, an if...else construct would be almost as efficient.
>>If
>>you have only a couple of options per case statement, case can
>>drop-through
>>like this:
>>
>>switch (i)
>>{
>> case 100:
>> case 101:
>> // do something
>> break;
>> case 1000:
>> case 1001:
>> case 1002:
>> // do something else
>> break;
>>}
>>
>>Thanks,
>>Michael C., MCDBA
>>
>>"Chris" <christianc@pandora.be> wrote in message
>>news:o2V9d.273844$rh2.14058566@phobos.telenet-ops.be...[color=darkred]
>>> Hi,
>>>
>>> can you specify a range in a switch - statement ?
>>>
>>> switch (i)
>>> {
>>> case 100 - 999 :
>>> // do something
>>> break;
>>> case 1000 - 9999:
>>> // do something else
>>> break;
>>> }
>>>
>>> Previous syntax is not allowed, I know,
>>> but can you solve this using 'switch' ? without having to provide a case
>>> statement for every possible value and without using 'if' ?
>>>
>>> thanks
>>> Christian
>>>
>>>
>>>[/color]
>>[/color]
>[/color]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.languages.csharp]
Roy Fine
Guest
 
Posts: n/a
#7: Nov 16 '05

re: switch-statement ???



[color=blue]
> Also you are aloud to[/color]

should be "allowed to"


Michael C
Guest
 
Posts: n/a
#8: Nov 16 '05

re: switch-statement ???


"Fall-through" is not supported. The correct term is "Stacked case labels".
I apologize for the terminology mix-up.

Thanks,
Michael C., MCDBA

"JuLiE Dxer" <MsJuLiE@verizon.net> wrote in message
news:ub4im0dl7ckq8ofju6sd5e57aiq4dme9cs@4ax.com...[color=blue]
> I thought fall-throughs in switch statements were not supported in C#
> (design choice).
>
> At least, that's what mentioned on page 224 of Microsoft's "Inside C#"
> by Tom Archer
>
> On Sat, 09 Oct 2004 17:32:29 GMT, "Michael C" <michaelc@nospam.org>
> wrote:
>[color=green]
> >Hi Chris,
> >
> >We were discussing the heck out of that a few months ago, and the answer[/color][/color]
is[color=blue][color=green]
> >(in C#), no. On the other hand, if you only have two ranges as in the
> >example you posted, an if...else construct would be almost as efficient.[/color][/color]
If[color=blue][color=green]
> >you have only a couple of options per case statement, case can[/color][/color]
drop-through[color=blue][color=green]
> >like this:
> >
> >switch (i)
> >{
> > case 100:
> > case 101:
> > // do something
> > break;
> > case 1000:
> > case 1001:
> > case 1002:
> > // do something else
> > break;
> >}
> >
> >Thanks,
> >Michael C., MCDBA
> >
> >"Chris" <christianc@pandora.be> wrote in message
> >news:o2V9d.273844$rh2.14058566@phobos.telenet-ops.be...[color=darkred]
> >> Hi,
> >>
> >> can you specify a range in a switch - statement ?
> >>
> >> switch (i)
> >> {
> >> case 100 - 999 :
> >> // do something
> >> break;
> >> case 1000 - 9999:
> >> // do something else
> >> break;
> >> }
> >>
> >> Previous syntax is not allowed, I know,
> >> but can you solve this using 'switch' ? without having to provide a[/color][/color][/color]
case[color=blue][color=green][color=darkred]
> >> statement for every possible value and without using 'if' ?
> >>
> >> thanks
> >> Christian
> >>
> >>
> >>[/color]
> >[/color]
>[/color]


ambshah
Guest
 
Posts: n/a
#9: Nov 16 '05

re: switch-statement ???


cant we use less than operator ??
case < 100
case <999
??


"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:ck9e8o$fqe$1@sparta.btinternet.com...[color=blue]
> "Chris" <christianc@pandora.be> wrote:
>[color=green]
>> switch (i)
>> {
>> case 100 - 999 :[/color]
>
> That will actually compile, but it won't do what you want. (The minus sign
> is subtraction, so it's equivalent to "case -899".)
>
> You can't specify ranges in a C# switch statement.
>
> P.
>
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#10: Nov 16 '05

re: switch-statement ???


ambshah <ambshah@hotmale.not4spam> wrote:[color=blue]
> cant we use less than operator ??
> case < 100
> case <999[/color]

No. You have to provide a single value per case.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
ambshah
Guest
 
Posts: n/a
#11: Nov 16 '05

re: switch-statement ???


in vb we used this technique a lot its neat

switch(true)

{case (10 < 100):

MessageBox.Show("First ");

break;

case (10> 100):

MessageBox.Show("Second");

break;

default:

MessageBox.Show("default");

break;

}


substitute variable in place of 10

hth
ambrish





"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:ck9e8o$fqe$1@sparta.btinternet.com...[color=blue]
> "Chris" <christianc@pandora.be> wrote:
>[color=green]
>> switch (i)
>> {
>> case 100 - 999 :[/color]
>
> That will actually compile, but it won't do what you want. (The minus sign
> is subtraction, so it's equivalent to "case -899".)
>
> You can't specify ranges in a C# switch statement.
>
> P.
>
>[/color]


Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#12: Nov 16 '05

re: switch-statement ???


LOL - I must have been in phonetic mode ;-)

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<OnI8hIvrEHA.3608@TK2MSFTNGP14.phx.gbl>


[color=blue]
> Also you are aloud to[/color]

should be "allowed to"



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.775 / Virus Database: 522 - Release Date: 08/10/2004



[microsoft.public.dotnet.languages.csharp]
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#13: Nov 16 '05

re: switch-statement ???


ambshah <ambshah@hotmale.not4spam> wrote:[color=blue]
> in vb we used this technique a lot its neat[/color]

Is that any better than:

if (i < 100)
{
....
}
else if (i > 100)
{
}

though?

If so, why?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Paul E Collins
Guest
 
Posts: n/a
#14: Nov 16 '05

re: switch-statement ???


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote:
[color=blue]
> Is [VB's Case x To y] any better than
> [if i ... else if i ...]
> If so, why?[/color]

Well, I'll play devil's advocate: the 'if - else if' form makes you
repeatedly specify the variable being tested, which could lead to an
obscure bug if you accidentally specify a different variable at some
point.

P.


Štefan Šimek
Guest
 
Posts: n/a
#15: Nov 16 '05

re: switch-statement ???



"Paul E Collins" <find_my_real_address@CL4.org> wrote in message
news:cke2hn$nor$1@hercules.btinternet.com...[color=blue]
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote:
>[color=green]
>> Is [VB's Case x To y] any better than
>> [if i ... else if i ...]
>> If so, why?[/color]
>
> Well, I'll play devil's advocate: the 'if - else if' form makes you
> repeatedly specify the variable being tested, which could lead to an
> obscure bug if you accidentally specify a different variable at some
> point.
>
> P.
>
>[/color]

On the other hand, if you have the variable mentioned on the top of the
switch and then you have 20 cases spread along four pages of code, you might
just wonder what your case 10-100: means.

Personally, I think that ranged cases allow for more bugs than multiple
if's.

Stefan


Closed Thread


Similar C# / C Sharp bytes