Noobie question about Switch

Discussion in 'C++' started by lallous, Dec 26, 2003.

  1. lallous

    lallous Guest

    Hello,

    "Aristotelis E. Charalampakis"
    <> wrote in message
    news:bshe3m$2vbi$...
    > Hi all, this is a newbie question :)
    >
    > I was wondering if there was a way to use the switch statement in a manner
    > that each case statement includes more that a simple value. i.e.:
    >
    >
    > switch ( myFloat )
    > {
    >
    > case >0: // ??? how do i write this ???
    >
    > // execute if myfloat >0
    >
    > break;
    >
    > case 0:
    >
    > // execute if myfloat ==0
    >
    > break;
    >
    > default:
    >
    > // you got the idea
    > }
    >
    > TIA
    > Aristotelis
    >
    >


    You can use if/else if/else combos or some sort of control table of your
    design:
    float range[3*2] = { 0.1, 0.9, 1, // if greater than 0.1 and below 0.9 then
    integer i = 1
    1.1, 2.1, 2}; // if 1.1 and 2.1 then 2, etc..

    --
    Elias
     
    lallous, Dec 26, 2003
    #1
    1. Advertising

  2. Hi all, this is a newbie question :)

    I was wondering if there was a way to use the switch statement in a manner
    that each case statement includes more that a simple value. i.e.:


    switch ( myFloat )
    {

    case >0: // ??? how do i write this ???

    // execute if myfloat >0

    break;

    case 0:

    // execute if myfloat ==0

    break;

    default:

    // you got the idea
    }

    TIA
    Aristotelis
     
    Aristotelis E. Charalampakis, Dec 26, 2003
    #2
    1. Advertising

  3. So it is not possible to write something like
    "case >0" ?

    Can it be written in the form

    switch ( TRUE )
    case myFloat>0:
    etc?

    In VB I can write something like this:

    select case myFloat
    case 2 , 3
    'Do something
    case Is > 4
    'do something else
    case else
    'default
    end select

    Or

    select case True
    case myFloat>4
    'Do something
    case myFloat<0
    'do something else
    case else
    'default
    end select

    Is there a way to implement the above with the switch statement?

    TIA,
    Aristotelis

    "lallous" <> wrote in message
    news:bshe9m$d61h6$-berlin.de...
    > Hello,
    >
    > "Aristotelis E. Charalampakis"
    > <> wrote in message
    > news:bshe3m$2vbi$...
    > > Hi all, this is a newbie question :)
    > >
    > > I was wondering if there was a way to use the switch statement in a

    manner
    > > that each case statement includes more that a simple value. i.e.:
    > >
    > >
    > > switch ( myFloat )
    > > {
    > >
    > > case >0: // ??? how do i write this ???
    > >
    > > // execute if myfloat >0
    > >
    > > break;
    > >
    > > case 0:
    > >
    > > // execute if myfloat ==0
    > >
    > > break;
    > >
    > > default:
    > >
    > > // you got the idea
    > > }
    > >
    > > TIA
    > > Aristotelis
    > >
    > >

    >
    > You can use if/else if/else combos or some sort of control table of your
    > design:
    > float range[3*2] = { 0.1, 0.9, 1, // if greater than 0.1 and below 0.9

    then
    > integer i = 1
    > 1.1, 2.1, 2}; // if 1.1 and 2.1 then 2,

    etc..
    >
    > --
    > Elias
    >
    >
     
    Aristotelis E. Charalampakis, Dec 26, 2003
    #3
  4. lallous

    Ron Natalie Guest

    "Aristotelis E. Charalampakis" <> wrote in message
    > In VB I can write something like this:
    >

    We're not in VB anymore.

    > select case myFloat
    > case 2 , 3

    In C/C++:
    case 2: case 3:

    There's no way to do the relationals directly. You could do something like:

    switch(i) {
    case 2:
    case 3:
    ...

    default:
    if(i > 10) { ...

    You can't do switch with floats at all. The arg has to be an integer or
    enum type.
     
    Ron Natalie, Dec 26, 2003
    #4
  5. lallous

    Rolf Magnus Guest

    Aristotelis E. Charalampakis wrote:

    > Hi all, this is a newbie question :)
    >
    > I was wondering if there was a way to use the switch statement in a
    > manner that each case statement includes more that a simple value.
    > i.e.:
    >
    >
    > switch ( myFloat )


    You cannot use switch/case on floating point values, only integral
    values.

    > {
    >
    > case >0: // ??? how do i write this ???
    >
    > // execute if myfloat >0
    >
    > break;
    >
    > case 0:
    >
    > // execute if myfloat ==0
    >
    > break;
    >
    > default:
    >
    > // you got the idea
    > }


    There is no way to do that. Just use if:

    if (myFloat == 0)

    // execut if myFloat == 0

    else if (myFloat > 0)

    // esecute if myFloat > 0

    else

    // you got the idea

    Btw: testing floating point values for equality is most often a bad idea
     
    Rolf Magnus, Dec 27, 2003
    #5
  6. Thank you all. I will stick to the if / else combos.

    > Btw: testing floating point values for equality is most often a bad idea


    Yes, it is a bad idea. I am not noobie in general, just in C++.

    Yet, I have persuaded myself to write some dlls (mostly math stuff) in C++,
    so that I get to know it. Some other dlls are in Visual Fortran 6.5, the gui
    in VB.

    Aristotelis.
     
    Aristotelis E. Charalampakis, Dec 27, 2003
    #6
  7. "Aristotelis E. Charalampakis" <> wrote in message news:<bshe3m$2vbi$>...
    > Hi all, this is a newbie question :)
    >
    > I was wondering if there was a way to use the switch statement in a manner
    > that each case statement includes more that a simple value. i.e.:


    A way of doing it (just as tiring as if-else though) is:

    switch (myFloat > 0) {
    case true: blah blah; break; //myfloat > 0
    case false: switch (myFloat == 0) {
    case true: blah blah; break; //myfloat == 0
    case false: blah blah; break; //myfloat < 0
    }
    break;
    }
     
    Chris Mantoulidis, Dec 27, 2003
    #7
    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:
    0
    Views:
    343
  2. Shaun
    Replies:
    2
    Views:
    564
    Simon Brunning
    Apr 22, 2005
  3. ProvoWallis

    noobie mkdir problem/question

    ProvoWallis, Mar 25, 2006, in forum: Python
    Replies:
    5
    Views:
    325
    Dennis Lee Bieber
    Mar 26, 2006
  4. =?Utf-8?B?VG9kZA==?=

    Noobie question: best way to maintain a list?

    =?Utf-8?B?VG9kZA==?=, Oct 23, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    320
    =?Utf-8?B?VG9kZA==?=
    Oct 23, 2006
  5. Bit Byte
    Replies:
    1
    Views:
    865
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page