Avoid if else...
-
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
-
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
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
-
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
-
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
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
-
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