In what situations is the foreach loop used in SystemVerilog?
In what situations is the foreach loop used in SystemVerilog?
Model answer
The foreach loop iterates over the elements of an array. It declares its loop variables automatically and takes the range from the array, so the code does not depend on the array's size. It works on fixed-size arrays, dynamic arrays, queues, associative arrays (except wildcard-indexed ones) and strings. Common uses include: Processing data: reading or updating every element of an array. Stimulus: applying each test vector stored in an array to the DUT. Checking: comparing actual results with an array of expected values. Constraints: applying a rule to each element of a random array.