|
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.
|
|
|
|
|
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...
|
|
|
|
|
I wouldn't subclass a if .
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]
|
|
|
|
|
Why not? It's simple
class MyIf : public if
{
};
IT'S FRIDAY!!!!!
|
|
|
|
|
And now saturday here :pizza: :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]
|
|
|
|
|
Wow, why would someone vote this a '1'?
|
|
|
|
|
|
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 for n in a range (1,10) all callfunc s would be called.
Greetings - Jacek Gajek
|
|
|
|
|
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
vicky
|
|
|
|
|
Try
#define WHATEVER_YOU_LIKE_AS_IF if
#define WHATEVER_YOU_LIKE_AS_ELSE else
and 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)
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]
|
|
|
|
|
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
|
|
|
|
|
One more advantage of this method i can change limits any time at one location for any if condition.
So to change the limits i need to see only at one place.
Thanks
Vikas
vicky
|
|
|
|
|
Hai!
Using MessageBox or AfxMessageBox we are able to display some message to the user during runtime, the message may include the value returned from a function etc.
Can we do the other way ??
I want the user should enter one byte data during run time, depending on the entered value i want to proceed my calculation .
Is it posible in MFC, if yes how to do it !!
Thanks!
|
|
|
|
|
kapardhi wrote: I want the user should enter one byte data during run time, depending on the entered value i want to proceed my calculation .
Is it posible in MFC, if yes how to do it !!
So why not just create a dialog box with an edit control?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Is there no other way other than creating a dialog !!!
Any inbuilt messagebox !!
thanks!
|
|
|
|
|
kapardhi wrote: Is there no other way other than creating a dialog !!!
No.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
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]
|
|
|
|
|
Instead of typing all these exclamation marks you could have already created that dialog.
|
|
|
|
|
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]
|
|
|
|
|
What are you smoking today?
|
|
|
|
|
Do you want to join the party?
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]
|
|
|
|
|