# Can we use Ternary operator in Constraints?

**URL:** <https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraints\
**Created:** [May 20, 2020, 9:37am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607 "2020-05-20T09:37:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arrpaduru](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/5f8ce5/32.png) [@arrpaduru](https://verificationacademy.com/forums/u/arrpaduru)\
**Post date:** [May 20, 2020, 9:37am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/1 "2020-05-20T09:37:15Z")

</div>

Hi,

Can we use the Ternary operator in constraints, instead of if-else? I tried using Ternary but the result is not as expected.

Here is an example code:

```verilog
class cons_check;
  rand logic [1:0] a;
  rand logic [3:0] b;
  
  constraint cons{
    solve a before b;
    b == (a<2) ? 2 : 3;
    //if (a<2) b == 2;
    //else b == 3;
  }
endclass

module tb();
  cons_check cons_h = new();
  initial begin
    cons_h.randomize() with{a == 1;};
    $display("a = %0d, b= %0d",cons_h.a, cons_h.b);
  end
endmodule

```

Output with Ternary: a = 1, b= 10  
Output with if-else: a = 1, b= 2

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 20, 2020, 3:32pm UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/2 "2020-05-20T15:32:06Z")

</div>

_In reply to [arrpaduru](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607):_

Your problem is the precedence of the equality ‘==’ operator is higher than the conditional operator ‘?:’. What you wrote gets interpreted as:

```verilog
( b == (a<2) ) ? 2 : 3;

```

This leaves b unconstrained since any value of b results in the expression being non-zero(true).

---

<div class="post-metadata">

**Author:** ![kitanisi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/f05b48/32.png) [@kitanisi](https://verificationacademy.com/forums/u/kitanisi)\
**Post date:** [May 21, 2020, 1:41am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/3 "2020-05-21T01:41:39Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/2):_

Dave’s answer is not correct.  
Correct constraint is

```verilog
b == ((a<2) ? 2 : 3);

```

---

<div class="post-metadata">

**Author:** ![MICRO\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@MICRO\_91](https://verificationacademy.com/forums/u/MICRO_91)\
**Post date:** [May 21, 2020, 1:50am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/4 "2020-05-21T01:50:03Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/3):_

Hi Dave ,

(1) If ‘==’ takes precedence shouldn’t b be constrained to 1 since ( a \< 2 ) would return 1 ( due to in-line constraint a == 1 ; )

(2) Also I didn’t get " since any value of b results in the expression being non-zero(true). "

What if I were to change constraint to ::

```verilog
 b == (a<2) ? 0 : 3; 

```

Would the 0 ( since a == 1 ) play any part ?

Thanks .

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 21, 2020, 2:53am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/5 "2020-05-21T02:53:30Z")

</div>

_In reply to [MICRO\_91](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/4):_

When A is constrained to be 1

```verilog
b == (a<2) ? 2 : 3;
       ⇩
b == 1 ? 2 : 3;
       ⇩
(b == 1) ? 2 : 3;

```

If the solver chooses b to be 1, then the result of this expression is 2, which satisfies the constraint because it is true (non-zero)

If the solver chooses b to a value other than 1, then the result of this expression is 3, which satisfies the constraint because it is true (non-zero)

The solver is free to choose any value for b.

If you change the constraint to

```verilog
b == (a<2) ? 0 : 3;
       ⇩
b == 1 ? 0 : 3;
       ⇩
(b == 1) ? 0 : 3;

```

If the solver chooses b to be 1, then the result of this expression is 0, which does not satisfy the constraint because it is false (zero)

If the solver chooses b to a value other than 1, then the result of this expression is 3, which satisfies the constraint because it is true (non-zero).

So b is contrainted to be a value other than 1.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 21, 2020, 3:00am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/6 "2020-05-21T03:00:01Z")

</div>

_In reply to [kitanisi](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/5):_

You are not correct. Please look at table 11-2 in the 1800-2017 LRM and find which operators have higher precedence. You can also make s simple test case and plug in values for a and b.

```verilog
$display (" the result of b == (a<2) ? 2 : 3 is %b", b == (a<2) ? 2 : 3. );

```

---

<div class="post-metadata">

**Author:** ![MICRO\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@MICRO\_91](https://verificationacademy.com/forums/u/MICRO_91)\
**Post date:** [May 21, 2020, 3:12am UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/7 "2020-05-21T03:12:08Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/6):_

Thanks Dave for the detailed explanation .

(b == 1) is actually the condition ( for ternary operator ) rather than a equality constraint .

---

<div class="post-metadata">

**Author:** ![juhi\_p](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@juhi\_p](https://verificationacademy.com/forums/u/juhi_p)\
**Post date:** [May 21, 2020, 5:25pm UTC](https://verificationacademy.com/forums/t/can-we-use-ternary-operator-in-constraints/37607/8 "2020-05-21T17:25:28Z")

</div>

```auto
b == ((a<2) ? 2 : 3);

```

will solve your problem.
