Noobie question about Switch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aristotelis E. Charalampakis

    Noobie question about Switch

    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


  • lallous

    #2
    Re: Noobie question about Switch

    Hello,

    "Aristoteli s E. Charalampakis"
    <aristotelis_ch aralampakis@REM OVEMEhotmail.co m> wrote in message
    news:bshe3m$2vb i$1@ulysses.noc .ntua.gr...[color=blue]
    > 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
    >
    >[/color]

    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


    Comment

    • Aristotelis E. Charalampakis

      #3
      Re: Noobie question about Switch

      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" <lallous@lgwm.o rg> wrote in message
      news:bshe9m$d61 h6$1@ID-161723.news.uni-berlin.de...[color=blue]
      > Hello,
      >
      > "Aristoteli s E. Charalampakis"
      > <aristotelis_ch aralampakis@REM OVEMEhotmail.co m> wrote in message
      > news:bshe3m$2vb i$1@ulysses.noc .ntua.gr...[color=green]
      > > Hi all, this is a newbie question :-)
      > >
      > > I was wondering if there was a way to use the switch statement in a[/color][/color]
      manner[color=blue][color=green]
      > > 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
      > >
      > >[/color]
      >
      > 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[/color]
      then[color=blue]
      > integer i = 1
      > 1.1, 2.1, 2}; // if 1.1 and 2.1 then 2,[/color]
      etc..[color=blue]
      >
      > --
      > Elias
      >
      >[/color]


      Comment

      • Ron Natalie

        #4
        Re: Noobie question about Switch


        "Aristoteli s E. Charalampakis" <aristotelis_ch aralampakis@REM OVEMEhotmail.co m> wrote in message[color=blue]
        > In VB I can write something like this:
        >[/color]
        We're not in VB anymore.
        [color=blue]
        > select case myFloat
        > case 2 , 3[/color]
        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.


        Comment

        • Rolf Magnus

          #5
          Re: Noobie question about Switch

          Aristotelis E. Charalampakis wrote:
          [color=blue]
          > 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 )[/color]

          You cannot use switch/case on floating point values, only integral
          values.
          [color=blue]
          > {
          >
          > case >0: // ??? how do i write this ???
          >
          > // execute if myfloat >0
          >
          > break;
          >
          > case 0:
          >
          > // execute if myfloat ==0
          >
          > break;
          >
          > default:
          >
          > // you got the idea
          > }[/color]

          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


          Comment

          • Aristotelis E. Charalampakis

            #6
            Re: Noobie question about Switch

            Thank you all. I will stick to the if / else combos.
            [color=blue]
            > Btw: testing floating point values for equality is most often a bad idea[/color]

            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.


            Comment

            • Chris Mantoulidis

              #7
              Re: Noobie question about Switch

              "Aristoteli s E. Charalampakis" <aristotelis_ch aralampakis@REM OVEMEhotmail.co m> wrote in message news:<bshe3m$2v bi$1@ulysses.no c.ntua.gr>...[color=blue]
              > 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.:[/color]

              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;
              }

              Comment

              Working...