Repeat Loop

Syntax: looping_statement::==
repeat (conditional) statement

A repeat loop executes a fixed number of times, the conditional can be a constant, variable or a signal value but must contain a number. If the conditional is a variable or a signal value, it is evaluated only at the entry to the loop and not again during execution. The jug and cups example in the previous section cannot be implemented using a repeat loop, Why? but array initialisation can be, How? The example below illustrates the loop.


        module comply;
          int count;
        // counting down from 128

        initial begin
          count = 128;
          repeat (count) begin
             $display("%d seconds to comply", count);
             count = count - 1;
          end
        end

        endmodule


Note: The loop will execute 128 times regardless of whether the value of count changes after entry to the loop.

EXERCISE
Think of a situation where the for loop would be prefered over a repeat loop.

Answers


previous next contents