Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Avoid if else...

Avoid if else...

Scheduled Pinned Locked Moved Managed C++/CLI
helpcode-review
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vikasvds
    wrote on last edited by
    #1

    Hello, Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

    vicky

    T L L 3 Replies Last reply
    0
    • V vikasvds

      Hello, Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

      vicky

      T Offline
      T Offline
      thoru
      wrote on last edited by
      #2

      Hello. In .NET you could do something like: Define an array of minlimits and maxlimits, define a class C to hold your methods, methods that are named in the line of M1, M2 and then use invokemember to call them like this: For(int i = 0;i<bound;i++) {    if((minlimits[i]<n)&and(maxlimits[i]>n)    C.GetType().InvokeMember("M"+i.tostring(), .....) } In C++ i think you can do this with an array of pointers to functions, but i quit C++ a long time ago and i don't know for sure. Also, the .NET solution is not tested, but looks good:P

      modified on Thursday, June 11, 2009 10:10 AM

      1 Reply Last reply
      0
      • V vikasvds

        Hello, Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

        vicky

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        vikasvds wrote:

        Please provide your inputs/help to help me finding other ways to optimize programming.

        Software Design Patterns[^]

        1 Reply Last reply
        0
        • V vikasvds

          Hello, Problem is - User can input any number, program need to take decision based on inputs. Let suppose input is stored in variable n; based on value of n some function will be called. program logic can be - if(n > 1 && n < 10 ) callfun1(); if(n > 11 && n < 20 ) callfun2(); if(n > 25 && n < 30 ) callfun3(); if(n > 33 && n < 38 ) callfun4(); if(n > 1 && n < 10 ) callfun5(); . . . and so on if(n > minlimit && n < maxlimit ) callfunX(); Is there any easy way to avoid if else chain to do similar work. Or is there any way to change the limits in if condition at some central place something using #define. Please provide your inputs/help to help me finding other ways to optimize programming. Thank Vikas

          vicky

          L Offline
          L Offline
          LionAM
          wrote on last edited by
          #4

          Hello! You could store the intervals in a sorted container together with the function delegates to call for the specific interval. You could now use a binary search to find the appropriate intervals. However, it gets more difficult when the intervals overlap and there has to be a certain order. When there are repeated intervals as above, you could use multicast delegates, calling callfun1() and callfun5() after each other. Alex

          V 1 Reply Last reply
          0
          • L LionAM

            Hello! You could store the intervals in a sorted container together with the function delegates to call for the specific interval. You could now use a binary search to find the appropriate intervals. However, it gets more difficult when the intervals overlap and there has to be a certain order. When there are repeated intervals as above, you could use multicast delegates, calling callfun1() and callfun5() after each other. Alex

            V Offline
            V Offline
            vikasvds
            wrote on last edited by
            #5

            Thanks for all of your responses. I have got the answer. Thanks Vikas

            vicky

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups