faq swith case can be a range?

F

fcvcnet

Hi,
In swith statements, follow case is constant-expression, can it be a
range?
For example:
...
case ' > 0 ' :
...
thanks.
 
G

Gavin Deane

fcvcnet said:
Hi,
In swith statements, follow case is constant-expression, can it be a
range?
For example:
...
case ' > 0 ' :
...
thanks.

No. Such a range is not an integral constant expression. Maybe you need
a chain of if-else if- ... -else.

Gavin Deane
 
D

Default User

Gavin said:
No. Such a range is not an integral constant expression. Maybe you
need a chain of if-else if- ... -else.

The usual way in C or C++ is to use "fall through".


switch (c)
{
case '>':
case '0':
// do stuff
break;
}


This quickly gets messy for large ranges, so using if-else if-else
ladders are prefered by many.




Brian
 
R

Robert Mabee

fcvcnet said:
In swith statements, follow case is constant-expression, can it be a
range?

It is a simple extension; BCPL, a C ancestor, had it in 1970, as in
case 'A' to 'Z':
If you are willing to use non-standard extensions, ask your compiler
vendor or get a public-source compiler and add it yourself.
 
B

BobR

fcvcnet wrote in message ...
Thanks you all.

A litle slow on the draw here. I found this in the GCC docs:
"Case Ranges"

case 1 ... 5: // must be a space before and after the '...'

case 'A' ... 'Z':

May NOT be portable, GCC 'feature'.

The GNU crew is usually good about saying whether it is an GCC extension, ISO
C89, C90, C99. But, they didn't say in this case, so, try it in your compiler
and watch the error output. Maybe, report if it works in a compiler other
than GCC (It compiles in my MinGW 3.3).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top