473,624 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

case labels

Hi,

why general integer expressions are not allowed in case labels in
switch statements..??? ?
Mar 28 '08 #1
55 3130
aa*****@gmail.c om wrote:
Hi,

why general integer expressions are not allowed in case labels in
switch statements..??? ?
Because they must be compile time constants.

--
Ian Collins.
Mar 28 '08 #2
On Mar 28, 10:03 am, Ian Collins <ian-n...@hotmail.co mwrote:
aark...@gmail.c om wrote:
Hi,
why general integer expressions are not allowed in case labels in
switch statements..??? ?

Because they must be compile time constants.

--
Ian Collins.
Could you elaborate this concept?
Mar 28 '08 #3
sumsin wrote:
On Mar 28, 10:03 am, Ian Collins <ian-n...@hotmail.co mwrote:
>aark...@gmail. com wrote:
>>Hi,
why general integer expressions are not allowed in case labels in
switch statements..??? ?
Because they must be compile time constants.
*Please* don't quote signatures.
>
Could you elaborate this concept?
The language requires case labels to be compile time constants. There
really isn't a concept to elaborate on.

--
Ian Collins.
Mar 28 '08 #4
Ian Collins <ia******@hotma il.comwrites:
sumsin wrote:
>On Mar 28, 10:03 am, Ian Collins <ian-n...@hotmail.co mwrote:
>>aark...@gmail .com wrote:
Hi,
why general integer expressions are not allowed in case labels in
switch statements..??? ?
Because they must be compile time constants.
*Please* don't quote signatures.
>>
Could you elaborate this concept?

The language requires case labels to be compile time constants. There
really isn't a concept to elaborate on.
Unless you want to know *why* the standard imposes this requirement.

The code generated for a switch statement is typically a static jump
table (though it doesn't have to be). The advantage of this is that
the code doesn't have to traverse through various possible values of
the controlling expression to determine where to jump; it's typically
faster than an equivalent if/else chain. The disadvantage is that the
values for all the labels have to be known at compile time so the
compiler can construct the jump table.

Having said that, there's nothing forbidding a compiler from treating
a switch statement like an if/else chain anyway (and it will probably
have to do something like that if the range of cases is very large).

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 28 '08 #5

<aa*****@gmail. comwrote in message
news:a3******** *************** ***********@m44 g2000hsc.google groups.com...
Hi,

why general integer expressions are not allowed in case labels in
switch statements..??? ?
There's no reason why switch expressions can't be any (identical) type, as
Keith has suggested.

If they are all constant integers, then fine generate a jump table.
Otherwise generate appropriate if-statements. This is very easy at a time
when C compilers are so super-sophisticated they can almost bake bread too.

But the people responsible for new C standards have decided to keep this
area of the language (control statements in general) at a primitive level.

So the programmer has to code his logic in a less expressive manner.

--
Bart
Mar 28 '08 #6

"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:r6******** *************** *******@comcast .com...
Bartc wrote:
>>
There's no reason why switch expressions can't be any (identical) type,
as Keith has suggested.

If they are all constant integers, then fine generate a jump table.
Otherwise generate appropriate if-statements. This is very easy at a time
when C compilers are so super-sophisticated they can almost bake bread
too.

But the people responsible for new C standards have decided to keep this
area of the language (control statements in general) at a primitive
level.

So the programmer has to code his logic in a less expressive manner.

If you switch on a double, with double case labels,
what's the matching criterion?
....
If you switch on a struct-valued expression, what's
the matching criterion?
....
If you switch on a `char*', should the matching
criterion be strcmp()?
Yes I know there's all sorts of obscure examples where such a switch would
be ambiguous, but in that case no advance would ever be made.

Probably all the OP wants is to be have variable int cases, then an
extension of switch which simply applies the == operator would be a useful
advance.

Another simple enhancement is a range operator (maybe gcc has this?) like
case 'A'..'Z':, which in the case of doubles could translate into >= and <=.
And with char* strings, the compiler can apply some common sense and use
strcmp(). There are ways.
If you've got ideas of how to handle questions of this
kind, get hold of the gcc source and implement your answers.
Then float it around to see whether people like it; if
they do, you'll have taken a big step towards getting it
into a revised Standard -- "prior art" is a lot more
convincing than "I wish."
I've worked on a compiler for a language where something similar is possible
(although I found it easier to have two forms of switch: one with constant
int cases, and one with any simple type where =/== made sense). And it was
workable.

But no I don't fancy delving into the gcc sources!

--
Bart
Mar 28 '08 #7
Eric Sosman wrote:

(fx:dreaming)
If you switch on a struct-valued expression, what's
the matching criterion?
struct_Foo_swit ch_equality
( struct Foo *switched, struct Foo *cased );

struct_Foo_hash _value
( struct Foo *foo );
If you switch on a `char*', should the matching
criterion be strcmp()? Or caseInsensitive Strcmp()? Or
allWhiteSpaceCo untsAsIdentical Strcmp()?
strcmp. If the user wants something else, they can canonise
their switched string first. (And I'm imagining a `strhash`.)

I grant that an implementation is more convincing than a handwave is.

Getting non-unfortunate performance might be more of a problem than
mere semantics. (Hence the hashes.)

--
"Its flagships were suns, its smallest vessels, /The City and the Stars/
planets."

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Mar 28 '08 #8
Bartc wrote:
"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:r6******** *************** *******@comcast .com...
>Bartc wrote:
>>There's no reason why switch expressions can't be any (identical) type,
as Keith has suggested.

If they are all constant integers, then fine generate a jump table.
Otherwise generate appropriate if-statements. This is very easy at a time
when C compilers are so super-sophisticated they can almost bake bread
too.

But the people responsible for new C standards have decided to keep this
area of the language (control statements in general) at a primitive
level.

So the programmer has to code his logic in a less expressive manner.
If you switch on a double, with double case labels,
what's the matching criterion?
...
> If you switch on a struct-valued expression, what's
the matching criterion?
...
> If you switch on a `char*', should the matching
criterion be strcmp()?

Yes I know there's all sorts of obscure examples where such a switch would
be ambiguous, but in that case no advance would ever be made.
The question is whether the "advance" produces something
useful, and the situations I mentioned seem to me like real-
life problems the enhanced switch would have trouble with.
Probably all the OP wants is to be have variable int cases, then an
extension of switch which simply applies the == operator would be a useful
advance.
The O.P. in this case asked a very short question, from
which you seem to derive a lot more information than I can.

Besides, I'm still not convinced of the utility, even
if restricted to int values. Try this:

int x = 42, y = 43;
for (int i = 40; i < 50; ++i) {
switch (i) {
case x: printf("x\n"); break;
case y: printf("y\n"); break;
default: printf("?\n"); break;
}
y = 42;
}

Worse still, try this:

int c1, c2;
c1 = getchar();
switch (c1) {
case EOF: printf("done!\n "); return;
case c2 = getchar():
printf("two %c's in a row\n", c1);
break;
default:
ungetc(c2); break;
}

Question: Is the "internal" getchar() executed if c1==EOF?

Another simple enhancement is a range operator (maybe gcc has this?) like
case 'A'..'Z':, which in the case of doubles could translate into >= and <=.
Yes, gcc has an extension like this. And yes, it's less
useful than one might think. For example, the case you show
does necessarily select all upper-case letters, nor does it
necessarily exclude all non-letters. What good is it?

Case ranges might be handy in some other situations, but
it seems suggestive that the example always offered for them
is the one you've shown, where ranges are clearly *not* the
right thing to use.
And with char* strings, the compiler can apply some common sense and use
strcmp(). There are ways.
... so the enhanced switch is limited to just one notion
of matching. True, it's probably the most "natural" notion
for strings, but it's not the only notion. Also, what do
you get if the char* you switch on (or one of the labels)
is NULL? What if it's just a char* that points at a char,
but not at the start of a string?
I've worked on a compiler for a language where something similar is possible
(although I found it easier to have two forms of switch: one with constant
int cases, and one with any simple type where =/== made sense). And it was
workable.
I'm not saying that definitions could not be invented to
nail down these loose bits; other languages have certainly
come up with switch-ish constructs that handle at least some
of the difficulties. But is it worth the trouble? I'm not
sure -- and that's why I suggested doing an implementation
and finding out whether people would use the feature in
interesting ways.
But no I don't fancy delving into the gcc sources!
Well, if the advance isn't worth some fairly serious
work, that may mean the need it addresses isn't too urgent.

--
Er*********@sun .com
Mar 28 '08 #9
aa*****@gmail.c om wrote:
>
why general integer expressions are not allowed in case labels in
switch statements..??? ?
Because a common switch implementation is an indexed jump through a
fixed table of destinations.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 28 '08 #10

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

Similar topics

3
1675
by: Martin Magnusson | last post by:
I'm using enums as case labels throughout my code, and that works fine in gcc 3.3.4. However, when compiling it with the compiler that ships with Dev-C++ 4.9.8.0, I get the error "case label does not reduce to an integer constant". The case label in question looks like this case I<D>::E: And I have a class template <unsigned D>
10
9567
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
3
4318
by: Grim Reaper | last post by:
I print mailing labels out of Access 2000 databases about 3 to 4 times a week. I have been having problems with one thing since I have been printing mailing labels. I print mailing labels by using a report I created and then printing them on a continuous-feed dot matrix printer. I used the Label Wizard, chose the query I created, picked the 'Avery USA 4146' (1 7/16" x 4") - 3 across, paper size: 'Fanfold 358mm x 12 in', and Printer:...
3
2327
by: Grim Reaper | last post by:
I know this is probably an easy question, but I could not find/figure it out. Basically, I am printing mailing labels with a "Sorting/Grouping" section that groups the label types together. Also, I am using a "Report Header" to show a count of how many total labels that are being printed. Now, my problem is that without the "Report Header", the spacing is perfect. But, when I add the Report Header, it gets shifted downwards and the...
5
2406
by: Frederick Dean | last post by:
Hi,guys! I'm reading Stephen Dewhurst's book "C++ Gotchas"£¬in gothca #7, I meet a weird case: bool Postorder::next() { switch (pc) case START: while (true) if (!child()) { pc = LEAF; return true;
6
8643
by: Ron | last post by:
Hi, I know Access allows for easy construction of a report setup to print labels from a table/query, etc. I've done that one. It works pretty well for what I need. However, is there an example anywhere that someone can point me to that is more flexible? And, I mean flexible in this regard:
0
986
by: Zytan | last post by:
I want other labels in the task list such as DEBUG, but then when I comment out Debug.WriteLine, it appears in the task list. Is there anyway for it to be case sensitive? Right now, it's almost useless, since the words I want to use come about it random comments all the time. It forces me to use non-words like TODO. Zytan
22
3156
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point numbers then added support for putting in lat/lon coordinates. I tried this little piece of code inside the OnChar function but compiler complained about missing ';' after "case _T('W'):"
9
10994
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to how a switch case like the following one is converted into a jump table by the compiler, assuming that it does, switch(i) { case 4 : { ... printf("4");
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6107
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.