[Next] [Up] [Previous]
Next: Conditionals Up: Basic concepts Previous: Wait statements

Loops

A loop of the form

  while(cond)
    block
executes block as long as the condition cond is true. If cond is false, it falls through to the next statement in exactly zero time. The last statement of block must be a wait statement. As an example,
reg [1:0] x;

initial x = 0;

always
  begin
    while(x < 3)
      begin
        x = x + 1;
        wait(1);
      end
    x = 0;
  end
results in the sequence 0,1,2,3,0,1,...for x.

A for loop, on the other hand, must have static upper and lower bounds, and is unrolled at compile time. Thus, for example,

  for(i = 0; i < 4; i = i + 1)
    block(i)
is exactly equivalent to
    block(0);
    block(1);
    block(2);
    block(3);
The block in this case need not contain a wait statement.

Ken McMillan
Fri Nov 6 22:15:28 PST 1998