Noobs question about ternary expression

Hello to all,

I have this probably silly question concerning the use of ternary expression.
I do understand how it works in its original form. What i wanted to ask is if the code i wrote below is okay to be used on an arduino (or am i ubusing it?). I have tested it on an uno board and it seems to work all right.
anyway here it is.

void setup() 
{
  pinMode (8, INPUT);
  pinMode (9, INPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (12, OUTPUT);
}

void loop()
{
  (digitalRead(8) ||  digitalRead(9)) ? (digitalWrite (10, HIGH), digitalWrite (11, LOW), digitalWrite (12, LOW)) : (digitalWrite (10, LOW), digitalWrite (11, HIGH), digitalWrite (12, HIGH));
  
}

It is supposed to turn high pin 10 if pins 8 or 9 are high and turn pins 11 and 12 low else it will turn pin 10 low and pins 11 and 12 high.

Thank you.

Shouldn’t the bigger point be code readability.

Making things obscure will not be helpful 2 months down the road.

Does it work ? I suspect not because

(digitalWrite (10, HIGH), digitalWrite (11, LOW), digitalWrite (12, LOW))

would normally only execute the last of the commands. You might get away with

{digitalWrite (10, HIGH); digitalWrite (11, LOW); digitalWrite (12, LOW)}

If you are worried then why not write it as a normal if/else ?

This would be much better for readability and maintainability:

void setup()
{
  pinMode (8, INPUT);
  pinMode (9, INPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (12, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH ||  digitalRead(9) == HIGH)
  {
    digitalWrite (10, HIGH);
    digitalWrite (11, LOW);
    digitalWrite (12, LOW);
  }
  else
  {
    digitalWrite (10, LOW);
    digitalWrite (11, HIGH);
    digitalWrite (12, HIGH);
  }
}

larryd:
Shouldn’t the bigger point be code readability.
Making things obscure will not be helpful 2 months down the road.

Agreed.

UKHeliBob:
Does it work ? I suspect not because

(digitalWrite (10, HIGH), digitalWrite (11, LOW), digitalWrite (12, LOW))

would normally only execute the last of the commands. You might get away with

{digitalWrite (10, HIGH); digitalWrite (11, LOW); digitalWrite (12, LOW)}

If you are worried then why not write it as a normal if/else ?

well it looks like it works.

ToddL1962:
This would be much better for readability and maintainability:

void setup()

{
  pinMode (8, INPUT);
  pinMode (9, INPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (12, OUTPUT);
}

void loop()
{
  if (digitalRead(8) == HIGH ||  digitalRead(9) == HIGH)
  {
    digitalWrite (10, HIGH);
    digitalWrite (11, LOW);
    digitalWrite (12, LOW);
  }
  else
  {
    digitalWrite (10, LOW);
    digitalWrite (11, HIGH);
    digitalWrite (12, HIGH);
  }
}

If there is nothing more than meets the eye, no functional difference then sure i will code it the traditional way.

UKHeliBob:
Does it work ? I suspect not because

(digitalWrite (10, HIGH), digitalWrite (11, LOW), digitalWrite (12, LOW))

would normally only execute the last of the commands.
...

Nope. "In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations."

#include <stdio.h>

int a, b, c, d;

int main()
{
    printf("Hello, World!\n");
    
    a = 2, b = 3, c = 4, d = 5;
    
    printf("%d %d %d %d \n", a, b, c, d);

    return 0;
}

Try it.

I do agree however that the construct is unusual and could cause others to be confused.

a7

My apologies for my misunderstanding, but the real explanation of what happens leaves me even more convinced that use of the ternary operator with multiple dependant statements should not be used even if it works

UKHeliBob:
My apologies for my misunderstanding, but the real explanation of what happens leaves me even more convinced that use of the ternary operator with multiple dependant statements should not be used even if it works

I guess i have to agree with you. I suppose that in terms of programming, it is wrong to be used like that.