Looping construct inside a constraint

Hi,
Can we have for loop construct inside a constraint ? I understand we can use foreach but I am not sure if we can use for.
Thanks,
Omkar

In reply to o-hassan:
Constraints are not procedural statements - they are a set of simultaneous equations. The foreach construct is used as both a looping construct in procedural code and as an iterative constraint that gets unrolled until simultaneous constraints. SystemVerilog does not allow the for construct in a constrain, but you can think of foreach as a special case of the for construct.

In reply to o-hassan:

you cant use for loop in constraint. Use for loop in post randomize method.

how to exit from foreach loop in constraint ?

A foreach constraint is not procedural code; it is a set of iterative constraints that get unrolled into individual equations. You can use the iterator variable in your equations to get the effect you want. For example, if you want the first 5 elements of an array to be 0:

rand int A[100];
constraint C{ foreach (A[i]) i<5 -> A[i]==0; }
1 Like