Looking for a solution to optimize the program fragment

Hi all.
the program fragment like to set just one mark HIGH and all others LOW, how to optimize it like for-loop?
Thanks
Adam

if (condationA){
  MarkA = 1;
  MarkB = 0;
  MarkC = 0;
  MarkD = 0;
}else if (condationB){
  MarkA = 0;
  MarkB = 1;
  MarkC = 0;
  MarkD = 0;
}else if (condationC){
  MarkA = 0;
  MarkB = 0;
  MarkC = 1;
  MarkD = 0;
}else if (condationD){
  MarkA = 0;
  MarkB = 0;
  MarkC = 0;
  MarkD = 1;
}

Give examples of what condationA thru condationD might be.

2 Likes

If you’re treating them as separate variables, zero all at the top of the if(), then only set the required 1s in each condition.

Or you could use an array of Marks[] in the same way…
Even a bit array to hold your 0-1 values.

2 Likes

Hello shanren

Consider:

// make names
enum Condition {A, B, C, D};
// make variables
int16_t condition;
// make structures
struct MARK
{
  Condition condition;
  int16_t value;
};
MARK mark[] {A, 0, B, 0, C, 0, D, 0};
// make application
void setup()
{
}
void loop()
{
  for (auto &mark_ : mark) condition == mark_.condition ? mark_.value = 1 : mark_.value = 0;
}

Have a nice day and enjoy coding in C++.

1 Like
    MarkA = MarkB = MarkC = MarkD = 0;

    switch (condition) {
    case CondA:
        MarkA = 1;
        break;

    case CondB:
        MarkB = 1;
        break;

    case CondC:
        MarkC = 1;
        break;

    case CondD:
        MarkD = 1;
        break;
    }

4 Likes

Do you need 4 separate variables?

Could you just use 1 bit to represent each condition?

uit8_t mark;

:
:

mark =0;

if( conditionA )  mark |= 0x01;
if( conditionB )  mark |= 0x02;
if( conditionC )  mark |= 0x04;
if( conditionD )  mark |= 0x08;

or

struct {
   unsigned int A: 1;
   unsigned int B: 1;
   unsigned int C: 1;
   unsigned int D: 1;
} mark;

: 
: 

if( conditionA )  mark.A = 1;
if( conditionB )  mark.B = 1;
if( conditionC )  mark.C = 1;
if( conditionD )  mark.D = 1;
1 Like

Thanks.
They are buttons or touch screen soft buttons.

Thanks.
I did this way, but seems something not right in my code or ? I got expected result till I used the way in #1 post.

Sorry, my code don't have " zero all at the top of the if(), " and just zero all at the header config, maybe this is the reason?

Tested the way here can't control individual mark's 0 or 1.

Thanks.
I'll check it.
Have a nice day.

Thanks.
It works!

Thanks.
They are manually controlled buttons.

MarkA=(condationA?1:0);
MarkB=(condationB?1:0);
MarkC=(condationC?1:0);
MarkD=(condationD?1:0);
3 Likes

You could treat all the Mark variables as bool, and just have:

MarkA = condationA;
MarkB = condationB;
MarkC = condationC;
MarkD = condationD;

So MarkA will be true or false corresponding to whether the condationA evaluates as true or false, etc ...

1 Like

Thanks.
I'll test it.

... or force it to be boolean:

MarkA = (bool)condationA;
MarkB = (bool)condationB;
MarkC = (bool)condationC;
MarkD = (bool)condationD;
1 Like

a program fragment can't show enough context.

for example instead of MarkA, MarkB, MarkC, MarkD ... consider one Mark[4]
or why 4 variables at all.
in binary that information would fit in a single byte variable also.

1 Like

That would be too simple. Just think of it: C++ uses byte for bool. Why would you want to make it smaller?

1 Like

On an AVR UNO, RAM is quite precious ...

1 Like

conditionA is likely a bool already

2 Likes

Indeed it has to be - as it was the condition of an if in the OP:

1 Like