faq swith case can be a range?

Discussion in 'C++' started by fcvcnet, Oct 16, 2006.

  1. fcvcnet

    fcvcnet Guest

    Hi,
    In swith statements, follow case is constant-expression, can it be a
    range?
    For example:
    ...
    case ' > 0 ' :
    ...
    thanks.
     
    fcvcnet, Oct 16, 2006
    #1
    1. Advertising

  2. fcvcnet

    Gavin Deane Guest

    fcvcnet wrote:
    > 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
     
    Gavin Deane, Oct 16, 2006
    #2
    1. Advertising

  3. fcvcnet

    Default User Guest

    Gavin Deane wrote:

    >
    > fcvcnet wrote:
    > > 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.


    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
     
    Default User, Oct 16, 2006
    #3
  4. fcvcnet

    Robert Mabee Guest

    fcvcnet wrote:
    > 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.
     
    Robert Mabee, Oct 16, 2006
    #4
  5. fcvcnet

    fcvcnet Guest

    Thanks you all.
     
    fcvcnet, Oct 17, 2006
    #5
  6. fcvcnet

    BobR Guest

    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).

    --
    Bob R
    POVrookie
     
    BobR, Oct 22, 2006
    #6
    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. mit
    Replies:
    1
    Views:
    409
    Scott M.
    Mar 31, 2006
  2. Luis Esteban Valencia

    Cant swith to Design view in ASPX file. Is this a bug?

    Luis Esteban Valencia, Jan 20, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    454
    Luis Esteban Valencia
    Jan 20, 2005
  3. rotary swith

    , Aug 24, 2006, in forum: VHDL
    Replies:
    0
    Views:
    565
  4. Lambda
    Replies:
    2
    Views:
    399
    James Kanze
    Jul 16, 2008
  5. PerlFAQ Server
    Replies:
    0
    Views:
    306
    PerlFAQ Server
    Feb 8, 2011
Loading...

Share This Page