M
Mark L Pappin
Keith Thompson said:And of course it would break every existing program that uses "cond"
as a keyword,
ITYM "as an identifier,".
mlp
Keith Thompson said:And of course it would break every existing program that uses "cond"
as a keyword,
Mark L Pappin said:ITYM "as an identifier,".
In what way would your altered switch be superior to an if/else tree?
The primary reason to use a switch is that it may be translated at
compile-time into a jump table, but that benefit goes away if the
statements are not known until run-time. Languages like Ruby have
switches with run-time case expressions because they delay the
translation until run-time, anyway; such languages are using the syntax
to mean something very different from what the C switch means. IOW,
languages that provide case statements of the form you've suggested do
so only because they *can't* do what C and C++ do.
How about:
cond {
(foo == bar): ...; break;
(!bar && (baz == boo)): ...; /* fallthru */
default: ...; break;
}
In pseudo-grammar:
cond { [<expr>|default: <stmt>*]* }
What will be next? "case >=7:"?
[email protected] said:Frankly I think ranges on the case constant expressions would be a
more useful addition while staying with the basic philosophy of the C
switch statement. IOW, "case 2...5:", or something along those
lines. But still not something I'm loosing sleep over...
[email protected] said:Frankly I think ranges on the case constant expressions would be a
more useful addition while staying with the basic philosophy of the C
switch statement. IOW, "case 2...5:", or something along those
lines. But still not something I'm loosing sleep over...
Willem said:Keith Thompson wrote:
) Then programmers will inevitably write
)
) case 'A' ... 'Z':
)
) which is non-portable (under EBCDIC it matches '\' and '}').
In which case, it would be relatively easy to add syntax similar to:
case [A-Z]:
Which would, of course, be portable.
Keith said:Then programmers will inevitably write
case 'A' ... 'Z':
Hallvard B Furuseth said:They do anyway. #define ISUPPER(c) ('A' <= (c) && (c) <= 'Z'). E.g. to
check for the ASCII (or 7-bit Unicode) letters regardless of locale.
Frankly I think ranges on the case constant expressions would be a
more useful addition while staying with the basic philosophy of the C
switch statement. IOW, "case 2...5:", or something along those
lines. But still not something I'm loosing sleep over...
Hendrik Schober said:And why exactly would that be worse than an 'if'-'else' chain
relying on ASCII?
Caveat: Sometimes ('A' <= c && c <= 'Z') *is* exactly what you want, if
you're writing deliberately non-portable code.
Harald van Dijk said:It doesn't need to be *deliberately* non-portable. If you were
implementing your own C library, on an ASCII-based machine with minimal
locale support, this could be the best way to write isupper.
I'd call that deliberately non-portable, at least if you know what
you're doing.
Harald van Dijk said:I read your message as suggesting non-portable had to be a goal for 'A'<=c
&& c<='Z' to be the right thing. If you meant that non-portable can be
okay, just that you need to be aware of it, then agreed.
Hendrik Schober said:Keith said:It wouldn't. [...]Hendrik Schober said:Keith Thompson wrote: [...]
Then programmers will inevitably write
case 'A' ... 'Z':
which is non-portable (under EBCDIC it matches '\' and '}').
And why exactly would that be worse than an 'if'-'else' chain
relying on ASCII?
Then I don't see how your above argument is valid.
Hendrik Schober said:Keith said:Since you snipped my argument, I have no idea why you disagree withHendrik Schober said:Keith Thompson wrote:
Keith Thompson wrote: [...]
Then programmers will inevitably write
case 'A' ... 'Z':
which is non-portable (under EBCDIC it matches '\' and '}').
And why exactly would that be worse than an 'if'-'else' chain
relying on ASCII?
It wouldn't. [...]
Then I don't see how your above argument is valid.
it.
Funny. My newsreader still shows it.
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.