switch statement

Discussion in 'C++' started by D, Dec 1, 2004.

  1. D

    D Guest

    Can a switch statement have an arithmetic attribute in it. For example
    switch (x / 10)
    {
    case a: place your statements here
    }


    Thanks
     
    D, Dec 1, 2004
    #1
    1. Advertising

  2. D wrote in news: in
    comp.lang.c++:

    > Can a switch statement have an arithmetic attribute in it. For example
    > switch (x / 10)
    > {
    > case a: place your statements here
    > }
    >
    >


    No idea what you mean by "arithmetic attribute in it", if you mean
    a non-compiletime-constant, then no.

    int const ten = 10;

    int main()
    {
    int x = 100, a = 10;
    switch ( x / 10 )
    {
    case a: // illegal must be a compile time constant
    case 10: // ok
    case ten; // also ok.
    }
    }

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 1, 2004
    #2
    1. Advertising

  3. D wrote:
    > Can a switch statement have an arithmetic attribute in it. For example
    > switch (x / 10)
    > {
    > case a: place your statements here
    > }


    Yes.

    V
     
    Victor Bazarov, Dec 1, 2004
    #3
  4. D

    Default User Guest

    D wrote:

    > Can a switch statement have an arithmetic attribute in it. For
    > example switch (x / 10)
    > {
    > case a: place your statements here
    > }



    You mean expression, not attribute. The answer is yes, as long as the
    expression yields an integer type. Assuming x is an integer type, then
    your example above will have integer division and all will be well.



    Brian
     
    Default User, Dec 1, 2004
    #4
  5. D

    Default User Guest

    Rob Williscroft wrote:

    > D wrote in news: in
    > comp.lang.c++:
    >
    > > Can a switch statement have an arithmetic attribute in it. For
    > > example switch (x / 10)
    > > {
    > > case a: place your statements here
    > > }
    > >
    > >

    >
    > No idea what you mean by "arithmetic attribute in it", if you mean
    > a non-compiletime-constant, then no.



    Ah, you read it as asking about the case labels. I confess that now I'm
    not clear what was meant. I took it to mean the controlling expression.
    You're right, of course, about case labels.




    Brian
     
    Default User, Dec 1, 2004
    #5
    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:
    1
    Views:
    436
    Enrique
    Feb 19, 2004
  2. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    6,037
  3. Replies:
    21
    Views:
    1,090
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    436
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

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

Share This Page