Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > switch { } - case for range

Reply
Thread Tools

switch { } - case for range

 
 
markpapadakis
Guest
Posts: n/a
 
      09-06-2006

I was checking out the C-FAQ and read here (
http://c-faq.com/misc/nonconstcase.html ) that:
" case labels are limited to single, constant, integral expression ".

However, I have been using case with ranges for a long while ( gcc,
VC++) so either the FAQ calls for an update or those two compilers
provide this functionality as an extension.

example;
switch (a)
{
case 1 ... 10:
// code
break;

case 11 ... 50:
// code
break;

case 800:
// code
break;

default:
// code
break;
}

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-06-2006
"markpapadakis" <(E-Mail Removed)> writes:

> I was checking out the C-FAQ and read here (
> http://c-faq.com/misc/nonconstcase.html ) that:
> " case labels are limited to single, constant, integral expression ".
>
> However, I have been using case with ranges for a long while ( gcc,
> VC++) so either the FAQ calls for an update or those two compilers
> provide this functionality as an extension.


It's an extension.
--
Go not to Usenet for counsel, for they will say both no and yes.
 
Reply With Quote
 
 
 
 
BRG
Guest
Posts: n/a
 
      09-06-2006
markpapadakis wrote:
> I was checking out the C-FAQ and read here (
> http://c-faq.com/misc/nonconstcase.html ) that:
> " case labels are limited to single, constant, integral expression ".
>
> However, I have been using case with ranges for a long while ( gcc,
> VC++) so either the FAQ calls for an update or those two compilers
> provide this functionality as an extension.
>
> example;
> switch (a)
> {
> case 1 ... 10:
> // code
> break;
>
> case 11 ... 50:
> // code
> break;
>
> case 800:
> // code
> break;
>
> default:
> // code
> break;
> }


This doesn't work with VC++ version 8 and I don't recall it working on
earlier versions either.

Brian Gladman

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      09-06-2006
markpapadakis wrote:
> I was checking out the C-FAQ and read here (
> http://c-faq.com/misc/nonconstcase.html ) that:
> " case labels are limited to single, constant, integral expression ".
>
> However, I have been using case with ranges for a long while ( gcc,
> VC++) so either the FAQ calls for an update or those two compilers
> provide this functionality as an extension.


[snip example]

C doesn't have ranges. What you describe is an extension, one which is
clearly documented as such in the gcc documentation. What version of
VC++ are you using that allows this?

Robert Gamble

 
Reply With Quote
 
markpapadakis
Guest
Posts: n/a
 
      09-06-2006

Robert Gamble wrote:
> markpapadakis wrote:
> > I was checking out the C-FAQ and read here (
> > http://c-faq.com/misc/nonconstcase.html ) that:
> > " case labels are limited to single, constant, integral expression ".
> >
> > However, I have been using case with ranges for a long while ( gcc,
> > VC++) so either the FAQ calls for an update or those two compilers
> > provide this functionality as an extension.

>
> [snip example]
>
> C doesn't have ranges. What you describe is an extension, one which is
> clearly documented as such in the gcc documentation. What version of
> VC++ are you using that allows this?
>
> Robert Gamble


I was wrong. It does not work with VC++. It has been a long while since
I used it and I thought it was supported there as well.

Thank you for the answer to the question.

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-06-2006
markpapadakis wrote:
> I was checking out the C-FAQ and read here (
> http://c-faq.com/misc/nonconstcase.html ) that:
> " case labels are limited to single, constant, integral expression ".
>
> However, I have been using case with ranges for a long while ( gcc,
> VC++) so either the FAQ calls for an update or those two compilers
> provide this functionality as an extension.


It's an extension.

--
Clark S. Cox III
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      09-06-2006
markpapadakis wrote:
> I was checking out the C-FAQ and read here (
> http://c-faq.com/misc/nonconstcase.html ) that:
> " case labels are limited to single, constant, integral expression ".
>
> However, I have been using case with ranges for a long while ( gcc,
> VC++) so either the FAQ calls for an update or those two compilers
> provide this functionality as an extension.
>
> example;
> switch (a)
> {
> case 1 ... 10:
> // code
> break;
>
> case 11 ... 50:
> // code
> break;
>
> case 800:
> // code
> break;
>
> default:
> // code
> break;
> }
>


What is your experience with this extension?

Is it really useful?

How many times did you use it?

I would be interested in knowing if it makjes sense to
implement it in lcc-win32.

jacob
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      09-06-2006
On Wed, 06 Sep 2006 22:09:45 +0200, in comp.lang.c , jacob navia
<(E-Mail Removed)> wrote:

>markpapadakis wrote:
>> switch (a)
>> {
>>

>
>What is your experience with this extension?


For what its worth, my experience is that its really useful if you're
a Visual Basic programmer trying to write C in the style of VB.

For myself I consider this A Very Bad Idea (tm), never ever try to use
one language in the idiom of another. Consider the early editions of
Num Rec in C as a classic case-study. I mean, why buy a scooter to
haul logs?


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
Reply With Quote
 
markpapadakis
Guest
Posts: n/a
 
      09-06-2006

jacob navia wrote:
> markpapadakis wrote:
> > I was checking out the C-FAQ and read here (
> > http://c-faq.com/misc/nonconstcase.html ) that:
> > " case labels are limited to single, constant, integral expression ".
> >
> > However, I have been using case with ranges for a long while ( gcc,
> > VC++) so either the FAQ calls for an update or those two compilers
> > provide this functionality as an extension.
> >
> > example;
> > switch (a)
> > {
> > case 1 ... 10:
> > // code
> > break;
> >
> > case 11 ... 50:
> > // code
> > break;
> >
> > case 800:
> > // code
> > break;
> >
> > default:
> > // code
> > break;
> > }
> >

>
> What is your experience with this extension?
>
> Is it really useful?
>
> How many times did you use it?
>
> I would be interested in knowing if it makjes sense to
> implement it in lcc-win32.
>
> jacob



I am using it quite often. Its useful it eliminates the need for
conditional statements ( wherever conditional statements can be used,
of course ) , provides for easier to manage/understand code and perhaps
reaps any performance benefits versus conditional statements.

Mark

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-07-2006
markpapadakis wrote:
> jacob navia wrote:
>> markpapadakis wrote:
>>
>>> I was checking out the C-FAQ and read here (
>>> http://c-faq.com/misc/nonconstcase.html ) that:
>>> " case labels are limited to single, constant, integral
>>> expression ".
>>>
>>> However, I have been using case with ranges for a long while
>>> ( gcc, VC++) so either the FAQ calls for an update or those
>>> two compilers provide this functionality as an extension.
>>>
>>> example;
>>> switch (a)
>>> {
>>> case 1 ... 10:
>>> // code
>>> break;
>>>
>>> case 11 ... 50:
>>> // code
>>> break;
>>>
>>> case 800:
>>> // code
>>> break;
>>>
>>> default:
>>> // code
>>> break;
>>> }

>>
>> What is your experience with this extension?
>>
>> Is it really useful?
>>
>> How many times did you use it?
>>
>> I would be interested in knowing if it makjes sense to
>> implement it in lcc-win32.

>
> I am using it quite often. Its useful it eliminates the need for
> conditional statements ( wherever conditional statements can be
> used, of course ) , provides for easier to manage/understand code
> and perhaps reaps any performance benefits versus conditional
> statements.


Why in heavens name use it and become non-portable, when the
portable code (following) is clearer:

if ((a >= 1) && (a <= 10)) firstcode();
else if ((a > 10) && (a <= 50)) secondcode();
else if (a == 800) thirdcode();
else defaultcode();

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
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
How can I transform source range to destination range that is thesame as source? Lambda C++ 2 07-16-2008 05:18 PM
'ArgumentError: bad value for range' for range of Times David Bird Ruby 1 06-23-2008 12:12 PM
range() is not the best way to check range? Summercoolness@gmail.com Python 46 07-25-2006 08:10 PM
Scene range vs dynamic range Robert Feinman Digital Photography 2 07-04-2005 09:30 PM
Range does not take an Range object. Tomoyuki Kosimizu Ruby 3 11-27-2003 12:42 AM



Advertisments