Is there a difference when using the ternary operator in always and assign statements?

50 Views Asked by At

I understand that the ternary operator can be used in both always statements and assign statements. In always Statements, Is it possible to have problems in the synthesis or compilation process when using the ternary operator instead of if~else?

I would appreciate it if you could answer even a small difference.

1

There are 1 best solutions below

0
dave_59 On

The behavior of the ternary operator ( A ? B : C ) is the same regardless of where it gets used. It may be used wherever an expression is allowed. You may be confusing the way the ternary operator selects between the B and C expressions, from the way an if/else branching statement ( if (A) stB; else stC;) selects between two different statements when the A expression is unknown (X). This is only a problem for simulation, not synthesis.

The case where A is unknown gets treated differently in the two constructs.

Because the ternary operator is used in an expression, it can evaluate both B and C expressions simultaneously. If they both have the same value, it can return that value as the result, otherwise it returns an unknown value.

The if/else construct can only execute one branch. The standard requires the else branch to be executed when the the A condition is unknown.