473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

case statement ranges...

Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.

Thanks,

--John Ratliff
Jan 29 '07 #1
8 3700
John Ratliff wrote:
Are case statement ranges standard C++ or a compiler extension?
They are not standard.
ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this
was a GNU extension. I want to remove them from my code if they are
non-standard.
You can always make sure by switching to "strict" mode, IIRC.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 29 '07 #2
Victor Bazarov wrote:
John Ratliff wrote:
>Are case statement ranges standard C++ or a compiler extension?

They are not standard.
>ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this
was a GNU extension. I want to remove them from my code if they are
non-standard.

You can always make sure by switching to "strict" mode, IIRC.

V
Thanks.

--John Ratliff
Jan 29 '07 #3

On Jan 29, 11:37 am, John Ratliff <u...@example.netwrote:
Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;

}I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.
I would say they are non-standard, i have never seen them in the C++
standard document.

Ivan
http://www.0x4849.net

Jan 30 '07 #4
On Mon, 29 Jan 2007 14:37:20 -0500, John Ratliff <us**@example.netwrote:
>Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

I thought they were standard, but I remember reading somewhere this was
a GNU extension. I want to remove them from my code if they are
non-standard.
Non-standard. Replace them with:

if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }

-dr
Jan 30 '07 #5
Dave Rahardja <as*@me.comwrote:
On Mon, 29 Jan 2007 14:37:20 -0500, John Ratliff <us**@example.netwrote:
>>Are case statement ranges standard C++ or a compiler extension?

ex:

switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}

Non-standard. Replace them with:

if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }
Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:

switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;

case 0xA:
/* ... */
case 0x23:
method2();
break;
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 30 '07 #6
On Jan 30, 7:25 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Dave Rahardja <a...@me.comwrote:
On Mon, 29 Jan 2007 14:37:20 -0500, John Ratliff <u...@example.netwrote:
>Are case statement ranges standard C++ or a compiler extension?
>ex:
>switch (somevar) {
case 0 ... 9: method1(); break;
case 0xA: ... 0x23: method2(); break;
}
Non-standard. Replace them with:
if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }

Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:

switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;

case 0xA:
/* ... */
case 0x23:
method2();
break;
}
Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.

Ivan
http://www.0x4849.net


Jan 30 '07 #7


On Jan 30, 4:46 pm, "Ivan Novick" <ivan.d.nov...@gmail.comwrote:
On Jan 30, 7:25 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Dave Rahardja <a...@me.comwrote:
On Mon, 29 Jan 2007 14:37:20 -0500, John Ratliff <u...@example.netwrote:
>>Are case statement ranges standard C++ or a compiler extension?
>>ex:
>>switch (somevar) {
> case 0 ... 9: method1(); break;
> case 0xA: ... 0x23: method2(); break;
>>}
Non-standard. Replace them with:
if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }
Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:
switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;
case 0xA:
/* ... */
case 0x23:
method2();
break;
}
Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.
No. When all cases are "dense", the compiler typically arranges the
code above to a jumptable - something like

if (somevar < 0 || somevar 0x23) goto L_done;
jump jump_table[somevar]

Jan 30 '07 #8


On Jan 30, 4:46 pm, "Ivan Novick" <ivan.d.nov...@gmail.comwrote:
On Jan 30, 7:25 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Dave Rahardja <a...@me.comwrote:
On Mon, 29 Jan 2007 14:37:20 -0500, John Ratliff <u...@example.netwrote:
>>Are case statement ranges standard C++ or a compiler extension?
>>ex:
>>switch (somevar) {
> case 0 ... 9: method1(); break;
> case 0xA: ... 0x23: method2(); break;
>>}
Non-standard. Replace them with:
if (somevar >= 0 && somevar <= 9) { method1(); }
else if (somevar >= 0xa && somevar <= 0x23) { method2(); }
Or alternately, you can use the "fallthrough" behavior of the case
statements, depending on how much you wish to type:
switch (somevar) {
case 0:
case 1:
case 2:
/* you can fill in the rest here */
case 9:
method1();
break;
case 0xA:
/* ... */
case 0x23:
method2();
break;
}
Umm, surely the generated machine code for this, must have to do more
comparisons if you have lots of cases than using ranges with greater
than and less than comparisons, hence this code should be slower.
Nope. Typically the code gets faster as the compiler will use a jump-
table: indexing a vector with somevar gives an instruction pointer
which gets jumped to. The only comparisons would be against minimum
and maximun value. For the example something like:

if (somevar < 0 || somevar 0x23) goto L_done;
goto jump_table[somevar]; // obviously pseudo-code here

/Peter

Jan 30 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: markpapadakis | last post by:
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...
7
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include...
55
by: aarklon | last post by:
Hi, why general integer expressions are not allowed in case labels in switch statements..????
12
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.