switch { } - case for range

Discussion in 'C Programming' started by markpapadakis, Sep 6, 2006.

  1. 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;
    }
     
    markpapadakis, Sep 6, 2006
    #1
    1. Advertising

  2. markpapadakis

    Ben Pfaff Guest

    "markpapadakis" <> 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.
     
    Ben Pfaff, Sep 6, 2006
    #2
    1. Advertising

  3. markpapadakis

    BRG Guest

    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
     
    BRG, Sep 6, 2006
    #3
  4. Re: switch { } - case for range

    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
     
    Robert Gamble, Sep 6, 2006
    #4
  5. Re: switch { } - case for range

    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.
     
    markpapadakis, Sep 6, 2006
    #5
  6. 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
     
    Clark S. Cox III, Sep 6, 2006
    #6
  7. markpapadakis

    jacob navia Guest

    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
     
    jacob navia, Sep 6, 2006
    #7
  8. On Wed, 06 Sep 2006 22:09:45 +0200, in comp.lang.c , jacob navia
    <> 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
     
    Mark McIntyre, Sep 6, 2006
    #8
  9. Re: switch { } - case for range

    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
     
    markpapadakis, Sep 6, 2006
    #9
  10. markpapadakis

    CBFalconer Guest

    Re: switch { } - case for range

    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>
     
    CBFalconer, Sep 7, 2006
    #10
  11. Re: switch { } - case for range

    In article <>,
    CBFalconer <> wrote:

    >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();


    That's fine in that case, but I have often had cases where there are a
    few large ranges and lots of single cases (e.g. when considering a
    character during the parsing of some expression). Of course you can
    split it into some tests followed by a switch, but ranges would be
    neater and allow the compiler to choose the most efficient set of
    comparisons.

    -- Richard
     
    Richard Tobin, Sep 7, 2006
    #11
  12. markpapadakis

    Eric Sosman Guest

    Re: switch { } - case for range

    Richard Tobin wrote On 09/07/06 08:01,:
    > In article <>,
    > CBFalconer <> wrote:
    >
    >
    >>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();

    >
    >
    > That's fine in that case, but I have often had cases where there are a
    > few large ranges and lots of single cases (e.g. when considering a
    > character during the parsing of some expression). Of course you can
    > split it into some tests followed by a switch, but ranges would be
    > neater and allow the compiler to choose the most efficient set of
    > comparisons.


    Are you looking for

    case 'a' ... 'z':
    case 'A' ... 'Z':
    process_alphabetic(ch);
    break;

    ? If so, CBF's solution (now using isalpha() in a test)
    is superior on several grounds, correctness among them.

    --
     
    Eric Sosman, Sep 7, 2006
    #12
  13. Re: switch { } - case for range

    In article <1157646365.318129@news1nwk>,
    Eric Sosman <> wrote:

    > Are you looking for
    >
    > case 'a' ... 'z':
    > case 'A' ... 'Z':
    > process_alphabetic(ch);
    > break;


    Something not unlike that, but I generally have Unicode values.

    >? If so, CBF's solution (now using isalpha() in a test)
    >is superior on several grounds, correctness among them.


    Presumably you are referring to the possibility of a system where the
    alphabetic characters are not consecutive, but on such a system
    isalpha() would not give the right results for my Unicode characters -
    I would have to use numeric ranges.

    -- Richard
     
    Richard Tobin, Sep 7, 2006
    #13
  14. Re: switch { } - case for range

    On 7 Sep 2006 12:01:14 GMT, in comp.lang.c ,
    (Richard Tobin) wrote:

    >In article <>,
    >CBFalconer <> wrote:
    >
    >>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();

    >
    >That's fine in that case, but I have often had cases where there are a
    >few large ranges and lots of single cases (e.g. when considering a
    >character during the parsing of some expression). Of course you can
    >split it into some tests followed by a switch, but ranges would be
    >neater and allow the compiler to choose the most efficient set of
    >comparisons.


    I would submit that the algo was probably flawed, if you found
    yourself in this situation.
    --
    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
     
    Mark McIntyre, Sep 7, 2006
    #14
  15. Re: switch { } - case for range

    In article <>,
    Mark McIntyre <> wrote:

    >>That's fine in that case, but I have often had cases where there are a
    >>few large ranges and lots of single cases (e.g. when considering a
    >>character during the parsing of some expression). Of course you can
    >>split it into some tests followed by a switch, but ranges would be
    >>neater and allow the compiler to choose the most efficient set of
    >>comparisons.


    >I would submit that the algo was probably flawed, if you found
    >yourself in this situation.


    Why is that?

    Here's an example. I want to classify Unicode characters according
    to whether they are XML 1.1 name characters, name start characters, or
    other. What is wrong with writing something like:

    /* See http://www.w3.org/TR/xml11/#NT-NameStartChar */
    switch(c)
    {
    case 0x3a: /* colon */
    case 0x41...0x5a: /* A-Z */
    case 0x5f: /* underscore */
    case 0x61...0x7a: /* a-z */
    case 0xC0...0xD6:
    case 0xD8...0xF6:
    case 0xF8...0x2FF:
    case 0x370...0x37D:
    case 0x37F...0x1FFF:
    case 0x200C...0x200D:
    case 0x2070...0x218F:
    case 0x2C00...0x2FEF:
    case 0x3001...0xD7FF:
    case 0xF900...0xFDCF:
    case 0xFDF0...0xFFFD:
    case 0x10000...0xEFFF:
    return NameStart;
    case 0x2d: /* hyphen */
    case 0x2e: /* full stop */
    case 0x30...0x39: /* 0-9 */
    case 0xb7:
    case 0x300...0x36f:
    case 0x203f...0x2040:
    return NameChar;
    default:
    return Other;
    }

    Obviously there are other ways to do it, but I don't see why this way
    (which directly reflects the standard it is implementing) is "flawed".

    -- Richard
     
    Richard Tobin, Sep 7, 2006
    #15
  16. markpapadakis

    boa Guest

    Re: switch { } - case for range

    * Richard Tobin wrote, On 07.09.2006 23:08:
    [snip]

    > case 0x10000...0xEFFF:


    Shouldn't that be 0xEFFFF?

    Boa
     
    boa, Sep 8, 2006
    #16
  17. markpapadakis

    Guest

    [OT] Re: switch { } - case for range

    CBFalconer wrote:
    > 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();


    Too bad C didn't keep the range syntax from BCPL. Then you could have
    used the even clearer:

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

    --
    Sven Axelsson
     
    , Sep 8, 2006
    #17
  18. markpapadakis

    CBFalconer Guest

    Re: [OT] Re: switch { } - case for range

    wrote:
    >
    > CBFalconer wrote:
    >
    >> 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();

    >
    > Too bad C didn't keep the range syntax from BCPL. Then you could
    > have used the even clearer:
    >
    > if (1 <= a <= 10) firstcode();
    > else if (10 < a <= 50) secondcode();
    > else if (a == 800) thirdcode();
    > else defaultcode();


    I disagree. The grammar needed to parse such constructs is a
    horror, and the result is all sorts of silly errors. SPL (Algol
    based HP System Programming Language for the HP3000) had that
    construct, and it created nothing but trouble.

    --
    Some informative links:
    news:news.announce.newusers
    http://www.geocities.com/nnqweb/
    http://www.catb.org/~esr/faqs/smart-questions.html
    http://www.caliburn.nl/topposting.html
    http://www.netmeister.org/news/learn2quote.html
     
    CBFalconer, Sep 8, 2006
    #18
  19. Re: switch { } - case for range

    On 7 Sep 2006 22:08:47 GMT, in comp.lang.c ,
    (Richard Tobin) wrote:

    >In article <>,
    >Mark McIntyre <> wrote:
    >
    >>I would submit that the algo was probably flawed, if you found
    >>yourself in this situation.

    >
    >Here's an example. I want to classify Unicode characters according
    >to whether they are XML 1.1 name characters, name start characters, or
    >other. What is wrong with writing something like:


    for starters, case{} is clearly the wrong method for this sort of
    process. It'd be a lot easier and clearer with an if statement.

    >Obviously there are other ways to do it, but I don't see why this way
    >(which directly reflects the standard it is implementing) is "flawed".


    *ahem*
    Directly implementing a standard is very likely NOT to be the most
    efficient or sensible way to write code. Standards are written to be
    understood by humans.
    --
    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
     
    Mark McIntyre, Sep 8, 2006
    #19
  20. Re: switch { } - case for range

    In article <>,
    Mark McIntyre <> wrote:

    >Directly implementing a standard is very likely NOT to be the most
    >efficient or sensible way to write code. Standards are written to be
    >understood by humans.


    So are programs, I hope.

    -- Richard
     
    Richard Tobin, Sep 9, 2006
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    971
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    401
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    155
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. David Bird
    Replies:
    1
    Views:
    213
    Tiago Macedo
    Jun 23, 2008
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    110
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page