How to avoid if else...
-
Hello, previously i have posted in wrong forum. 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, previously i have posted in wrong forum. 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
You could arrange them in such a way that they effectively do a binary search. That wouldn't bring the total number of tests down but it would run faster (singe every time execution flows through it it can only take 1 path, so O(log n) steps instead of O(n)) Or you could fill a giant array with function pointers for O(1) time. Or maybe you could change the functions to not need to be selected anymore (using funny bit magic if necessary) but whether that's possible depends on the function of course.
-
Hello, previously i have posted in wrong forum. 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
a little help is the use of the keyword else :-O if(n > 1 && n < 10 ) { callfun1(); } else if(n > 11 && n < 20 ) .... and sort it so that the most likely cases coming first. AFAIK the compiler resolves switch in a similar way. Best is the use of enums. :cool:
Press F1 for help or google it. Greetings from Germany
-
Hello, previously i have posted in wrong forum. 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, previously i have posted in wrong forum. 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
You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
There are three kinds of people in the world - those who can count and those who can't...
-
You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
There are three kinds of people in the world - those who can count and those who can't...
molesworth wrote:
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Here are some quotes from Kent Beck in his book Implementation Patterns[^]
If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.
-
Hello, previously i have posted in wrong forum. 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
Have you something personal against
if-else
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
molesworth wrote:
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Here are some quotes from Kent Beck in his book Implementation Patterns[^]
If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.
led mike wrote:
The more paths through a program the less likely the program is to be correct
Aye, that's very true, and anything that can reduce complexity will also reduce the likelihood of bugs, and improve readability and, more importantly, testability. However, sometimes there's no way to improve on an if/else construct, and when used sensibly they're the right tools for the job. When you get into a morass of nested if/elseif/elseif... blocks, then you certainly do introduce problems. Sheesh! Next thing you'll be telling me "goto" is a bad idea :)
There are three kinds of people in the world - those who can count and those who can't...
-
molesworth wrote:
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Here are some quotes from Kent Beck in his book Implementation Patterns[^]
If/then and switch statements are the simplest form of instance –specific behavior…. The more paths through a program the less likely the program is to be correct….The proliferation of conditionals reduces reliability….This problem is compounded when conditionals are duplicated. These problems can all be eliminated by converting the conditional logic to messages, either with subclasses or delegation.
I wouldn't subclass a
if
. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I wouldn't subclass a
if
. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You probably need to take a step back, and think about what you're trying to achieve, and lay it out in a more logical pattern. Perhaps you can convert the value 'n' into a unique parameter for a switch statement. There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
There are three kinds of people in the world - those who can count and those who can't...
molesworth wrote:
There's also nothing wrong with using "else" so I'm not sure why you'd want to avoid it...
Adding
else
s to this code would dramatically change it's flow. Note that forn
in a range(1,10)
allcallfunc
s would be called.Greetings - Jacek Gajek
-
And now saturday here :beer: :beer: :pizza: :beer: :beer: :whisky:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hello, previously i have posted in wrong forum. 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
You can try something like this int func1() { return 0; } int func2() { return 0; } typedef int (*funcptr)(); struct condition { int low; int high; funcptr func; }; condition arr[] = { { 10, 20, func1 }, { 20, 30, func2 } // Can add more data here }; int n=17; for (int i=0;i<2;++i) { if (n > arr[i].low && n < arr[i].high) { arr[i].func(); // If you want you can break here } } -Arun
-
You can try something like this int func1() { return 0; } int func2() { return 0; } typedef int (*funcptr)(); struct condition { int low; int high; funcptr func; }; condition arr[] = { { 10, 20, func1 }, { 20, 30, func2 } // Can add more data here }; int n=17; for (int i=0;i<2;++i) { if (n > arr[i].low && n < arr[i].high) { arr[i].func(); // If you want you can break here } } -Arun
Thank you very much all of your response, The approach Arun has suggested is i think the way i was looking for as best and fasted approach. Thanks Arun to help me suggesting this approach, it's really nice and definitely fastest method uses full potential of C/C++ language. Thanks again. Vikas
vicky
-
Thank you very much all of your response, The approach Arun has suggested is i think the way i was looking for as best and fasted approach. Thanks Arun to help me suggesting this approach, it's really nice and definitely fastest method uses full potential of C/C++ language. Thanks again. Vikas
vicky
-
You ignored the responses you got on this question the first time. Reposting is rude. Perhaps you can explain why you ignored the previous responses and still reposted the same question.
I am very sorry for my re-post. I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C. I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category. Thank Vikas
vicky
-
Have you something personal against
if-else
?If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
CPallini wrote:
Have you something personal against if-else?
Yes, i don't like if-else I hate them X|
vicky
Try
#define WHATEVER_YOU_LIKE_AS_IF if
#define WHATEVER_YOU_LIKE_AS_ELSE elseand then, for instance
WHATEVER_YOU_LIKE_AS_IF (i>0 && i<10)
//..
WHATEVER_YOU_LIKE_AS_ELSE WHATEVER_YOU_LIKE_AS_IF (i>11 && i<20)
//..
//...;P
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
And now saturday here :beer: :beer: :pizza: :beer: :beer: :whisky:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I am very sorry for my re-post. I have re-posted my question because at other discussion thread i was getting responses in terms of C# and .Net, but i need response in terms of pure C. I have not done it intentionally, because of web page movement by mistake i've clicked at C# discussion link and posted the question instead of C/C++ category. Thank Vikas
vicky