My gut feeling says there is another way ( perhaps quite different to your approach) but I don't know what question you are trying to answer. Can you describe that in plain language (i.e. without any code).
i wanted to know if there is another way to combin them all into the if statement, i know the bottom code doesnt work, it was just an example of what i wanted it to do....
the top code is the same as the bottom but uses the "t.dow==" for each byte and i wanted to do something that doesnt have to repeat it self 7 times, hope that makes sense
switch (t.dow)
{
case timerSetSu1:
case timerSetMo1:
case timerSetTu1:
case timerSetWe1:
case timerSetTh1:
case timerSetFr1:
case timerSetSa1:
// do something
break;
}
bryanmc1988:
i wanted to know if there is another way to combin them all into the if statement,
I know that's how you want to improve the code. But it does not tell me what the code is intended for.
I would like to see a description in this style "check if ABC and do XYZ" - a plain language description of the effect that the code will have when it works. The sort of description that would make sense to your Mum.
Having timerSetSu, timerSetMo, etc.. is mad. Make them into an array.
Something like this
daytimer[7]={whatever, whatever, whatever, etc..};
for(n=0;n<7;n++)
if(t.dow==daytimer[n])
{//do your thing here
}
//But it would be better still to know what day it is, Then you could simply do
if(t.dow==daytimer[TODAY])
doYourThing();
Is there any other way to code the if statement to make it more clean?
No. The C language does not provide a way to abbreviate that statement.
If you want to test if a equals b, c or d, (where a,b,c and d are variables) you have to write each test for equality explicitly:
if(a == b || a == c || a == d) {...}
This doesn't work:
if(a == b || c || d) {...}
because it means "if a equals b OR c is non-zero OR d is non-zero"
and this also won't work:
if(a == b, c, d) {...}
Although this evaluates 'a==b' (unless the compiler optimizes it out), it does not use it. This statement is the same as if(d) - look up the comma operator.
If you have such a long if statement, chances are there's a better way to code the whole problem as KenF alludes to in #8.