Prev Page Next Page

Loop Statements

Formal Definition

Loop statements provide a means of modeling blocks of procedural statements.

Simplified Syntax

forever statement;

repeat (expression) statement;

while (expression) statement;

for (assignment; expression; assignment) statement;

Description

There are four types of loop statements: forever, repeat, while, and for statements.

The forever instruction (Example 1) continuously repeats the statement that follows it. Therefore, it should be used with procedural timing controls (otherwise it hangs the simulation).

The repeat instruction (Example 2) executes a given statement a fixed number of times. The number of executions is set by the expression, which follows the repeat keyword. If the expression evaluates to unknown, high-impedance, or a zero value, then no statement will be executed.

The while instruction (Example 3) executes a given statement until the expression is true. If a while statement starts with a false value, then no statement will be executed.

The for instruction (Example 4) executes a given statement until the expression is true. At the initial step, the first assignment will be executed. At the second step, the expression will be evaluated. If the expression evaluates to an unknown, high-impedance, or zero value, then the for statement will be terminated. Otherwise, the statement and second assignment will be executed. After that, the second step is repeated.

 

Examples

Example 1

always begin
  counter = 0;
  forever #10 counter = counter + 1;
end

Example 2

initial begin
  repeat (10) a = a + ~b;
end

Example 3

module test;
parameter MSB = 8;
reg [MSB-1:0] Vector;
integer t;
initial

 
begin
  t = 0;
  while (t < MSB)
    begin
      //Initializes vector elelments
      Vector[t] = 1'b0;
      t = t + 1;
    end
end
endmodule

 

Example 4

initial begin
  for (index=0; index < 10; index = index + 2)
  mem[index] = index;
end

 

Important Notes

 
Prev Page Next Page
Powered by IXwebhosting