A program needs to make multiple decisions, based on five variables (voltage, current, temp1, temp2 and temp3). Each decision (switching a LED or relay, writing text to an LCD,..) depends on some of these variables. How would you conceptualise the decision making process:
use multiple "if" statements
use single "if" and multiple "else-if" statements
use "switch"
others?
What would be the pro and con of each of the above decision makers?
Thank you for your time!
PS: I edited line "2.": changed "else" to "else-if"
You need to take each situation independently, and choose the method that's best for that particular situation.
For instance, if more than one variable is needed in the conditional, you can't use 'switch', since it only accepts a single variable.
'if' 'else', or 'if' 'else if' is usually preferable to individual 'if' statements, but again, it really depends on the situation. Sometimes 'if' is all that's required.
I am always cautious with complex IF statements (such as IF X && Y && Z) because there are 2N options and it is easy to get confused about which of them is triggering the IF. And a corresponding ELSE is even more confusing.
In general I prefer to cascade the IFs like
IF X
IF Y
IF Z
It also gives you the opportunity to identify the dominant IF and put that first so that time is not wasted testing irrelevant cases.
Hi,
Don't overthink the process of programming, don't worry about process time, there is not enough program memory to make a big enough sketch to make process time a factor in most cases, and avoid using delay function.
Just remember to write your code in stages, get each stage working before combining them to your final program.
We need to know the exact application to give more accurate suggestions.
Robin2:
I am always cautious with complex IF statements (such as IF X && Y && Z) because there are 2N options and it is easy to get confused about which of them is triggering the IF. And a corresponding ELSE is even more confusing.
In general I prefer to cascade the IFs like
IF X
IF Y
IF Z
It also gives you the opportunity to identify the dominant IF and put that first so that time is not wasted testing irrelevant cases.
...R
I didn't cover that situation in my reply, but when there are only two or three conditions, I like:-
if((x==1) and (y==2) and (y==4))
{
}
but I agree that it gets confusing when there are too many.
Is there a difference in processing time between multiple "if" statements versus "if" and multiple "else" statements?
Multiple solitary 'if' statements will all be tested every loop, and so will take longer. (But as Tom says, often that's unimportant.)
TomGeorge:
Hi,
Don't overthink the process of programming, don't worry about process time, there is not enough program memory to make a big enough sketch to make process time a factor in most cases, and avoid using delay function.
Just remember to write your code in stages, get each stage working before combining them to your final program.
We need to know the exact application to give more accurate suggestions.
Thanks.. Tom...
Hello Tom,
the application is a simple program driving a LCD display for a lab PSU showing voltage, current and some temperatures; also fast monitoring for short circuits (interacting with disconnection relay) and temperature spikes.
OldSteve:
I didn't cover that situation in my reply, but when there are only two or three conditions, I like:-
if((x==1) and (y==2) and (y==4))
{
}
but I agree that it gets confusing when there are too many.
Multiple solitary 'if' statements will all be tested every loop, and so will take longer. (But as Tom says, often that's unimportant.)
OldSteve, at most four conditions to test in each "if"
Even 3 tests in one IF statement means that there are 8 options and only one of them will trigger a TRUE response. However any or all of the other 7 will trigger the ELSE clause and that can lead to all sorts of strange behaviour.
IMHO cascading them makes the code much clearer to the programmer - and the compiler won't care.
Robin2:
Even 3 tests in one IF statement means that there are 8 options and only one of them will trigger a TRUE response. However any or all of the other 7 will trigger the ELSE clause and that can lead to all sorts of strange behaviour.
IMHO cascading them makes the code much clearer to the programmer - and the compiler won't care.
...R
In my example, I wasn't looking at cases where an 'else' is nested. In that case, your method is needed, but when it's a straight 'if' conditional without 'else', or you just want an 'else' to execute if all conditions are false, both of our methods would compile to the same code.
Then it's just a matter of preference.
As I said in my first reply, it really depends on the exact situation, because a nested else is more complex.
Here's a couple of the variations, including a nested 'else'. I really do find the shorter form with 'and' (or &&) easier to read:-
////////////////////////////////////////////////
if ((a == 1) and (b == 3) and (c == 0))
x = 2;
// is the same as:-
if (a == 1)
{
if (b == 3)
{
if (c == 0)
x = 2;
}
}
////////////////////////////////////////////////
if ((a == 1) and (b == 3) and (c == 0))
x = 2;
else
x = 3;
// is the same as:-
if (a == 1)
{
if (b == 3)
{
if (c == 0)
x = 2;
}
}
else
x = 3;
////////////////////////////////////////////////
if ((a == 1) and (b == 3))
{
if (c == 0)
x = 2;
else
x = 3;
}
// is the same as:-
if (a == 1)
{
if (b == 3)
{
if (c == 0)
x = 2;
else
x = 3;
}
}
////////////////////////////////////////////////
but it's really horses for courses. Neither is right, and neither is wrong.
The compiler has to split a complex condition into multiple tests. So it doesn't make any difference whether you combine multiple conditions into one, or test every single condition in multiple statements.
Make your code as clean and readable as possible, don't worry about speed with every statement. Consider that, if you really hit speed problems later, other people may have to look at your code and understand it.
the program fetches A and B and does the comparison.
if you have
if(A >= B) do this
if(A >= C) do that
the program fetches A and B and does the comparison.
the program fetches A and C and does the comparison.
if you have
if(A >=B)
else
if (A >=C)
the program fetches A and B and does the comparison.
the program already knows A and does not fetch it again for the else.
I am sure that you will find other places to save time, and I have to agree with Robin2 that simple is often easier to understand.
The other point is that your question is like asking if addition is better than multiplication.
there are times when one is the perfect fit and you have to work hard to do it another way.
often we call that an elegant solution.
the program fetches A and B and does the comparison.
the program fetches A and C and does the comparison.
Sorry, that's nonsense
The compiler is much cleverer than you seem to think. Values are cached in registers, accessible in following code, instructions may be reordered to remove redundant computations, and many more optimizations are applied.
And all that occurs at every single compilation, where the coder would have to re-think his optimizations after every single modification.
The compiler is much cleverer than you seem to think. Values are cached in registers, accessible in following code, instructions may be reordered to remove redundant computations, and many more optimizations are applied.
And all that occurs at every single compilation, where the coder would have to re-think his optimizations after every single modification.
gotta run to work, but I will get you the reference.
If your conditions are random, in other words, there is no real pattern that you can exploit, you can use a switch. Simply OR all your conditions together and switch on that:
// Condition number is bit number, 0 is LSB
state = CONDITION0 | CONDITION1 |CONDITION2|CONDITION3 |CONDITION4
switch code
{
case 0b00000:
//stuff
break;
case 0b00001:
//
break;
// ....30 more cases
}
An important point is none of this, it is to understand state-machines. Yes, learn
to code clearly, but a lot of microcontroller code is state-machines - so get clear on them
and ways to code them up.
KeithRB:
If your conditions are random, in other words, there is no real pattern that you can exploit, you can use a switch. Simply OR all your conditions together and switch on that:
// Condition number is bit number, 0 is LSB
state = CONDITION0 | CONDITION1 |CONDITION2|CONDITION3 |CONDITION4
I don't understand that, it will result in either true or false. IMO it should read